Re: Different Objects in Array
On Jan 9, 3:32 pm, Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
On Jan 9, 7:09 am, James Kanze <james.ka...@gmail.com> wrote:
On Jan 8, 1:21 pm, Michael Doubez <michael.dou...@free.fr> wrote:
On 8 jan, 04:51, Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
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, you have
seen this before. I created two classes of A and B. Two classes have
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 function
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...
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?
You can use union:
class Obj
{
public:
void Run()
{
this->*(pO[0].runA)();
this->*(pO[1].runB)();
That shouldn't compile, given the union you defined below; this
isn't the right type. (It's also lacking some parentheses.)
}
private:
A a;
B b;
union To
{
void A::*runA();
void B::*runB();
};
static To const pO[2];
};
// not sure about initialisation - perhaps only possible in C99
Obj::To const Obj::pO[2] =
{
{&A::Run},
{&B::Run}
};
You could use assignment somewhere, e.g. in an if in the
constructor. But once you've gotten the correct types in the
array, you have to call the function with the correct type; the
whole point of his exercise was to lie to the compiler, in order
to ensure that it generated incorrect code (and probably to use
some exotic and generally slow technique, like pointers to
members, rather than the usual faster technique of virtual
functions).
He is correct to point that Michael's post about union is not
working and C++ Compiler does not compile.
My question is always unanswered when I did research to find
out pointer to member function array.
Why member function pointer is **rarely** used in any C++
class?
Mainly because it has an awkward syntax, and generally, there
are other solutions which result in easier to maintain code.
Note too that you need special options in order for pointer to
member functions to work correctly with some C++ compilers
(VC++, for example).
[...]
Compare example 1 and example 5. I am unable to tell the speed
measurement because both example 1 and example 5 are very close.
In general, the speed won't make a difference. Don't worry
about it; get the code working and maintainable first, then
worry about speed (at least at this level).
Back to James Kanze' note --
You could use assignment somewhere, e.g. in an if in the
constructor. But once you've gotten the correct types in the
array, you have to call the function with the correct type;
the whole point of his exercise was to lie to the compiler,
in >order to ensure that it generated incorrect code (and
probably to use some exotic and generally slow technique,
like pointers to members, rather than the usual faster
technique of virtual functions).
How do you say *the usual faster technique of virtual
function*? class foo is the base class. All 256 classes are
derived from base class foo. They do not have composition.
They enable to use inheritance and polyporphism.
The virtual function has indirection overhead because it uses
indirection twice every time virtual function is invoked.
Maybe. A pointer to member funtion involves several
indirections as well. Generally more than a virtual function
(but implementations vary).
I need to decide whatever I want to choose either example 4 or
example 5 or I have to use inheritance and polymorphism.
In the end of my writing. Why pointer to member function is rarely
used?
As I said above, they're rather awkward to use, and like
pointers to functions, can quickly lead to unmaintainable code.
--
James Kanze