| View previous topic :: View next topic |
| Author |
Message |
jsnX Guest
|
Posted: Sun Oct 23, 2005 10:03 am Post subject: pointers casting up|down |
|
|
i can't seem to get inheritance|polymorphism to work. i create a vector
of base* like so:
vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
base::overridden_function() and not derived::overridden_function()!
what am i missing?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Eric Meijer Guest
|
Posted: Mon Oct 24, 2005 12:11 am Post subject: Re: pointers casting up|down |
|
|
jsnX wrote:
| Quote: | i can't seem to get inheritance|polymorphism to work. i create a vector
of base* like so:
vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
|
I would think that should give a compiler error. You might call
stuff[n]->overridden_function() though.
| Quote: | base::overridden_function() and not derived::overridden_function()!
what am i missing?
|
The problem would be in the derived or base class definition. Maybe you
simply forgot to declare the "overridden_function()" virtual in the
_base class_, or maybe the return type or argument list do not exactly
match between the base and derived classes.
Anyway, without the relevant base and derived class code, it is not
possible to determine what went wrong.
Kind regards,
Eric
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Mon Oct 24, 2005 12:16 am Post subject: Re: pointers casting up|down |
|
|
jsnX wrote:
| Quote: | i can't seem to get inheritance|polymorphism to work. i create a vector
of base* like so:
vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
base::overridden_function() and not derived::overridden_function()!
what am i missing?
Your program isn't well formed. stuff[n] is a pointer, you can't |
apply the . operator to it. So your problem is really one of
two things:
1. You need to call stuff[n]->overriden_function(). If you are
getting base::overriden_function, perhaps you forgot to declare
it virtual in the base class?
2. You really declared vector<base> rather than vector<base*>.
In this case you've copied the base portions of vector into the
base object (called slicing) in the vector. The resultant object
isn't a derived any more, so it has to call the base version of
the function.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joe Guest
|
Posted: Mon Oct 24, 2005 12:18 am Post subject: Re: pointers casting up|down |
|
|
"jsnX" <jason.dusek (AT) gmail (DOT) com> wrote
| Quote: | i can't seem to get inheritance|polymorphism to work. i create a vector
of base* like so:
vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
base::overridden_function() and not derived::overridden_function()!
what am i missing?
|
stuff[n].overridden_function() probably was not declared a virtual function.
class base{
void overridden_function(); // what you probably did
virtual void overridden_function(); // what you should have done
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pierre Barbier de Reuille Guest
|
Posted: Mon Oct 24, 2005 12:18 am Post subject: Re: pointers casting up|down |
|
|
jsnX a écrit :
| Quote: | i can't seem to get inheritance|polymorphism to work. i create a vector
of base* like so:
vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
base::overridden_function() and not derived::overridden_function()!
what am i missing?
|
Well, try sending actual code that don't work !
.... and try to get it as small as possible !
Because I see no flow in what you wrote above ... but there are many
other ways to get wrong ...
Pierre
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alec Ross Guest
|
Posted: Mon Oct 24, 2005 12:20 am Post subject: Re: pointers casting up|down |
|
|
In article <1130016998.380235.71990 (AT) g49g2000cwa (DOT) googlegroups.com>, jsnX
<jason.dusek (AT) gmail (DOT) com> writes
| Quote: | i can't seem to get inheritance|polymorphism to work. i create a vector
of base* like so:
vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
base::overridden_function() and not derived::overridden_function()!
what am i missing?
Are you declaring overridden function as virtual in the base? |
--
Alec Ross
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Mon Oct 24, 2005 12:23 am Post subject: Re: pointers casting up|down |
|
|
jsnX wrote:
| Quote: | i can't seem to get inheritance|polymorphism to work. i create a vector
of base* like so:
vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
base::overridden_function() and not derived::overridden_function()!
what am i missing?
|
A 'virtual' keyword, looks like.
--
A. Kanawati
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Mon Oct 24, 2005 12:23 am Post subject: Re: pointers casting up|down |
|
|
jsnX wrote:
| Quote: | i can't seem to get inheritance|polymorphism to work. i create a vector
of base* like so:
vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
|
Shoulnd't it be stuff[n]->overridden_function()?
| Quote: | base::overridden_function() and not derived::overridden_function()!
what am i missing?
|
Did you declare overridden_function() virtual in class base?
HTH,
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Valentin Samko Guest
|
Posted: Mon Oct 24, 2005 1:09 am Post subject: Re: pointers casting up|down |
|
|
jsnX wrote:
| Quote: | i can't seem to get inheritance|polymorphism to work. i create a vector
of base* like so:
vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
base::overridden_function() and not derived::overridden_function()!
|
Is your function virtual? Can you post an example which compiles?
--
Valentin Samko - http://val.samko.info
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Mon Oct 24, 2005 1:10 am Post subject: Re: pointers casting up|down |
|
|
"jsnX" <jason.dusek (AT) gmail (DOT) com> wrote
| Quote: | vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
base::overridden_function() and not derived::overridden_function()!
what am i missing?
|
Most likely is that you need to make base::overridden_function virtual, and
ensure that it has the same signature in the derived class.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Mon Oct 24, 2005 1:11 am Post subject: Re: pointers casting up|down |
|
|
jsnX wrote:
| Quote: | i can't seem to get inheritance|polymorphism to work. i create a vector
of base* like so:
vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
base::overridden_function() and not derived::overridden_function()!
what am i missing?
|
Nothing obvious. The code you show doesn't cause it, as you should see if
you removed the vector and instead only used a single pointer.
I suspect your derived class' function signature doesn't match the base
class' function signature, but that's just a guess.
Uli
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ismail Pazarbasi Guest
|
Posted: Mon Oct 24, 2005 1:11 am Post subject: Re: pointers casting up|down |
|
|
Is base::overridden_function a virtual function?
class base
{
public:
virtual void overridden_function() { /* ... */ }
};
If it's a virtual function, the call should go deep down to the last
override.
class A
{
public:
virtual void f() { /* ... */ }
};
class B : public A
{
public:
virtual void f() { /* ... */ }
};
class C : public B
{
};
class D : public C
{
};
vector<A*> g_vA;
int main()
{
int i;
for (i = 0; i < 4; i++)
{
D* pd = new D;
g_vA.push_back(pd);
}
for (i = 0; i < 4; i++)
{
D* pd = g_vA[i];
pd->f();
}
}
For D, the last override is at B::f, so the call should go B::f.
Ismail
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Mon Oct 24, 2005 1:12 am Post subject: Re: pointers casting up|down |
|
|
"jsnX" <jason.dusek (AT) gmail (DOT) com> writes:
| Quote: | i can't seem to get inheritance|polymorphism to work.
|
Please post a minimal (<50 lines), but complete program that
demonstrates your problem.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
jsnX Guest
|
Posted: Mon Oct 24, 2005 8:55 am Post subject: Re: pointers casting up|down |
|
|
ah ha! the signatures are different:
struct base
{
virtual void overridden_function() const
{
// do nothing
}
};
struct derived : public base
{
virtual void overridden_function()
{
// do something!
}
};
thank you all for encouraging me to go back and look at the code. it's
strange how can we sometimes look and look and look and not see.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Heinz Ozwirk Guest
|
Posted: Mon Oct 24, 2005 11:40 am Post subject: Re: pointers casting up|down |
|
|
"jsnX" <jason.dusek (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1130016998.380235.71990 (AT) g49g2000cwa (DOT) googlegroups.com...
| Quote: | i can't seem to get inheritance|polymorphism to work. i create a vector
of base* like so:
vector<base*> stuff;
for ( int i = 0; i < 4; ++i )
{
derived* foo = new derived(...args...);
stuff.push_back(foo);
}
but when i then call stuff[n].overridden_function() it calls
base::overridden_function() and not derived::overridden_function()!
|
If you really try to call stuff[n].overridden_function(), you should get a
compiler error, something like "base* does not have a member
overridden_function". You probably wanted to write
struff[n]->overridden_function().
Polymorphism only works with virtual functions. Make shure you declared
overridden_function as virtual in base, like
class base
{
...
virtual void overridden_function();
...
};
HTH
Heinz
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|