Re: Different Objects in Array
On Jan 7, 8:21 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
Immortal Nephi wrote:
I thought that you might find an interesting code. Perhaps, y=
ou have
seen this before. I created two classes of A and B. Two classes h=
ave
different objects. The class Obj has a relationship to class A and
class B which is called composition.
The class Obj has one variable which is a pointer to member func=
tion
in array. You can't bind member function of both class A and class B
because they do not belong to class Obj. I use reinterpret_cast to
convert from class A to class Obj before pointer to member function is
invoked.
I believe that my code is the alternative replacement so I don't=
use
inheritance and polymorphism. It is easier to extract class. What=
do
you think?
Your code has undefined behaviour, not to mention that the compiler that
is supposed to compile your 'reinterpret_cast' will exhibit undefined
behaviour (most likely). Aside from that, you're good...
Hi Victor,
How did you say undefined behavior? I played with debugging. I found
out that reinterpret_cast array did assign memory address correctly,
but *this* pointer in class A and class B assigns to the wrong memory
address.
For example:
class ....
{
int a;
int b;
int c;
}
*this* pointer should always assigns to data member as a, but it did
assigns to either data member as b or c. The data was misplaced from
a to b like memory misalignment.
I guess there is no alternative replacement to undefined
reinterpret_cast array, but switch block is the answer to each class'
member function.
Do you know if there is another way? What about template array?