By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

polymorphism - virtual function

P: n/a
al
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!
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"al" <al***@168.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
Jul 22 '05 #2

P: n/a
Victor Bazarov wrote:
"al" <al***@168.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.

Jul 22 '05 #3

P: n/a
"al" <al***@168.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?
Jul 22 '05 #4

P: n/a
al
Victor Bazarov <v.********@comAcast.net> wrote in message
news:ugrIb.706451$Fm2.609354@attbi_s04...
"al" <al***@168.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?
Jul 22 '05 #5

P: n/a
"al" <al***@168.net> wrote...
Victor Bazarov <v.********@comAcast.net> wrote in message
news:ugrIb.706451$Fm2.609354@attbi_s04...
"al" <al***@168.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
Jul 22 '05 #6

P: n/a
"al" <al***@168.net> wrote in message
news:dm***********************@bgtnsc04-news.ops.worldnet.att.net...
Victor Bazarov <v.********@comAcast.net> wrote in message
news:ugrIb.706451$Fm2.609354@attbi_s04...
"al" <al***@168.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/
Jul 22 '05 #7

P: n/a
Billy O'Connor <bi*****@gnuyork.org> wrote in message news:<87************@dps11.gnuyork.org>...
"al" <al***@168.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.
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.