Re: virtual functions and dynamic casting
Christopher Pisz wrote:
[..]
All the pertinant code was there,
I don't doubt it. I just wasn't ready to do all the work formatting
it and making it compile just to find out that I am missing plenty
of something else. If you need our help, you have to work a little
bit for it.
I really don't think you want 900
lines in a post.
Of course not!
But if it makes it easier, here is a fictionous
example:
Doesn't compile/link either.
class A
{
public:
A();
virtual ~A(){};
virtual int DoStuff();
};
class B : public A
{
public:
B();
virtual ~B(){};
int DoStuff();
};
class C
{
public:
C()
{
m_handler[0] = NULL;
m_handler[1] = NULL;
}
void Register(A * who)
{
if(!m_handler)
This check is bogus. In an instance of 'C', 'm_handler' will
*never* be NULL.
m_handler = who;
No assignment to arrays is possible. Did you mean
m_handler[0] = who;
?
else
m_handler[2] = who;
I am guessing you meant
m_handler[1] = who;
because 'm_handler' array doesn't have the element with index '2'.
}
void SomeFunction()
{
m_handler[0]->DoStuff();
m_handler[1]->DoStuff();
}
private:
A * m_handler[2];
};
C g_c;
A::A()
{
g_c.Register(this);
}
B:B()
B::B()
{
g_c.Register(this);
}
int main()
{
B b;
g_c.SomeFunction();
return 0;
}
B::DoStuff() is getting called twice, even though B registered an
instance of itself as a B pointer and a pointer of itself as an A
pointer.
It doesn't matter. You register the same object twice, and calling
a virtual function in the base class will lead to the _real_ object's
type function (the final overrider) to be called.
Since the base class registered itself, why isn't the Base
class's DoStuff() getting called? I thought the purpose of virtual
functions was that at run time, the appropriate function would get
called based upon what kind of pointer you are working with.
Not "working with", but "as created".
If it is
a pointer to a B than B's function gets called and if it is a pointer
to an A than A's function should get called. So, why isn't "this" in
A being treated as a pointer to an object of the A type and thusly
having it's version of DoStuff() called?
<shrug> I am guessing you don't understand the meaning or purpose
of virtual functions, or polymorphism for that matter.
Polymorphism in its primitive way of resolving the behaviour (your
'DoStuff' function) to the type with which the object was originally
created, won't work for you. I think you need to describe [to us]
what you are trying to accomplish with your registering. It seems
that you need to (a) make two unrelated classes AA and BB and make
them both inherit from A, and then (b) make your B class *contain*
two objects of each class ('AA' and 'BB'), and when 'B' is created
have it register both contained instances:
----------------------------------------------------------------
#include <cstdlib>
#include <iostream>
class A
{
public:
virtual ~A() {}
virtual int DoStuff() = 0;
};
class AA : public A {
int DoStuff() {
std::cout << "A::DoStuff()\n";
return 111;
}
};
class BB : public A
{
public:
int DoStuff() {
std::cout << "B::DoStuff()\n";
return 222;
}
};
class B
{
AA aa;
BB bb;
public:
B();
};
class C
{
public:
C()
{
m_handler[0] = NULL;
m_handler[1] = NULL;
}
void Register(A * who)
{
if(!m_handler[0])
m_handler[0] = who;
else
m_handler[1] = who;
}
void SomeFunction()
{
m_handler[0]->DoStuff();
m_handler[1]->DoStuff();
}
private:
A * m_handler[2];
};
C g_c;
B::B()
{
g_c.Register(&aa);
g_c.Register(&bb);
}
int main()
{
B b;
g_c.SomeFunction();
return 0;
}
----------------------------------------------------------------
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask