 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
al Guest
|
Posted: Wed Dec 31, 2003 3:15 am Post subject: polymorphism - virtual function |
|
|
class Base
{
public:
virtual void method();
};
class Derive : public Base
{
public:
void method();
};
Base *b = new Base;
b->method();//Base::method() called
Base *d = new Derive;
d->method();//Derive::method() called
Why b->method() trigger Base::method() whereas d->method() Derive::method()?
Thanks!
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Dec 31, 2003 3:20 am Post subject: Re: polymorphism - virtual function |
|
|
"al" <allin (AT) 168 (DOT) net> wrote...
| Quote: | class Base
{
public:
virtual void method();
};
class Derive : public Base
{
public:
void method();
};
Base *b = new Base;
b->method();//Base::method() called
Base *d = new Derive;
d->method();//Derive::method() called
Why b->method() trigger Base::method() whereas d->method()
Derive::method()? |
Because 'b' is a pointer to a complete object of class Base, and
'd' is a pointer to a subobject of a complete object of class Derive.
What matters is the type of the object at its creation (the actual,
the complete, object).
V
|
|
| Back to top |
|
 |
Jeff Schwab Guest
|
Posted: Wed Dec 31, 2003 3:26 am Post subject: Re: polymorphism - virtual function |
|
|
Victor Bazarov wrote:
| Quote: | "al" <allin (AT) 168 (DOT) net> wrote...
class Base
{
public:
virtual void method();
};
class Derive : public Base
{
public:
void method();
};
Base *b = new Base;
b->method();//Base::method() called
Base *d = new Derive;
d->method();//Derive::method() called
Why b->method() trigger Base::method() whereas d->method()
Derive::method()?
Because 'b' is a pointer to a complete object of class Base, and
'd' is a pointer to a subobject of a complete object of class Derive.
What matters is the type of the object at its creation (the actual,
the complete, object).
V
|
What Victor said is true for any "virtual" method, including the one
you've called here. If you want to change the behavior, just don't
include the word "virtual" in the method declaration.
|
|
| Back to top |
|
 |
Billy O'Connor Guest
|
Posted: Wed Dec 31, 2003 3:31 am Post subject: Re: polymorphism - virtual function |
|
|
"al" <allin (AT) 168 (DOT) net> writes:
| Quote: | class Base
{
public:
virtual void method();
};
class Derive : public Base
{
public:
void method();
};
Base *b = new Base;
b->method();//Base::method() called
Base *d = new Derive;
d->method();//Derive::method() called
Why b->method() trigger Base::method() whereas d->method() Derive::method()?
|
Well, b is a Base, and d is a Derived. What behavior were you
expecting?
|
|
| Back to top |
|
 |
al Guest
|
Posted: Wed Dec 31, 2003 4:35 am Post subject: Re: polymorphism - virtual function |
|
|
Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote
| Quote: | "al" <allin (AT) 168 (DOT) net> wrote...
class Base
{
public:
virtual void method();
};
class Derive : public Base
{
public:
void method();
};
Base *b = new Base;
b->method();//Base::method() called
Base *d = new Derive;
d->method();//Derive::method() called
Why b->method() trigger Base::method() whereas d->method()
Derive::method()?
Because 'b' is a pointer to a complete object of class Base, and
'd' is a pointer to a subobject of a complete object of class Derive.
What matters is the type of the object at its creation (the actual,
the complete, object).
V
Thanks! This definitely helps me understand the topic. |
If removing "virtual" from method() declaraton of class Base, then why
d->method() triggers Base::method() since 'd' is a pointer to the actual
object of class Derive?
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Dec 31, 2003 4:17 pm Post subject: Re: polymorphism - virtual function |
|
|
"al" <allin (AT) 168 (DOT) net> wrote...
| Quote: | Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:ugrIb.706451$Fm2.609354 (AT) attbi_s04 (DOT) ..
"al" <allin (AT) 168 (DOT) net> wrote...
class Base
{
public:
virtual void method();
};
class Derive : public Base
{
public:
void method();
};
Base *b = new Base;
b->method();//Base::method() called
Base *d = new Derive;
d->method();//Derive::method() called
Why b->method() trigger Base::method() whereas d->method()
Derive::method()?
Because 'b' is a pointer to a complete object of class Base, and
'd' is a pointer to a subobject of a complete object of class Derive.
What matters is the type of the object at its creation (the actual,
the complete, object).
V
Thanks! This definitely helps me understand the topic.
If removing "virtual" from method() declaraton of class Base, then why
d->method() triggers Base::method() since 'd' is a pointer to the actual
object of class Derive?
|
No, 'd' is not a pointer to the actual object of class Derive. It is,
as I already said, a pointer to a subobject. If you don't use 'virtual'
in declaring the member function[s], the binding is static and does not
respect the fact that the subobject of type Base is really part of the
object created as 'Derive'.
The difference, hence, is static binding versus dynamic binding.
Victor
|
|
| Back to top |
|
 |
Cy Edmunds Guest
|
Posted: Wed Dec 31, 2003 4:31 pm Post subject: Re: polymorphism - virtual function |
|
|
"al" <allin (AT) 168 (DOT) net> wrote
| Quote: | Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:ugrIb.706451$Fm2.609354 (AT) attbi_s04 (DOT) ..
"al" <allin (AT) 168 (DOT) net> wrote...
class Base
{
public:
virtual void method();
};
class Derive : public Base
{
public:
void method();
};
Base *b = new Base;
b->method();//Base::method() called
Base *d = new Derive;
d->method();//Derive::method() called
Why b->method() trigger Base::method() whereas d->method()
Derive::method()?
Because 'b' is a pointer to a complete object of class Base, and
'd' is a pointer to a subobject of a complete object of class Derive.
What matters is the type of the object at its creation (the actual,
the complete, object).
V
Thanks! This definitely helps me understand the topic.
If removing "virtual" from method() declaraton of class Base, then why
d->method() triggers Base::method() since 'd' is a pointer to the actual
object of class Derive?
|
Well, that's the distinction between virtual and not virtual! If not virtual
the compiler only takes note of the declared type of the pointer and
effectively bit slices down to a Base type (which is OK because a Derive IS
a Base). If virtual it looks up the actual type and proceeds accordingly.
By the way, Derive::method is virtual also. Once a method is declared
virtual you can't make it non-virtual later.
--
Cy
http://home.rochester.rr.com/cyhome/
|
|
| Back to top |
|
 |
P.V.S.S.VIKAS Guest
|
Posted: Thu Jan 01, 2004 10:29 am Post subject: Re: polymorphism - virtual function |
|
|
Billy O'Connor <billyoc (AT) gnuyork (DOT) org> wrote
| Quote: | "al" <allin (AT) 168 (DOT) net> writes:
class Base
{
public:
virtual void method();
};
class Derive : public Base
{
public:
void method();
};
Base *b = new Base;
b->method();//Base::method() called
Base *d = new Derive;
d->method();//Derive::method() called
Why b->method() trigger Base::method() whereas d->method() Derive::method()?
Well, b is a Base, and d is a Derived. What behavior were you
expecting?
|
these lines have no sense in
i'am new to this world.learning to mve around kindly coperate.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|