polymorphism - virtual function

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!
 
V

Victor Bazarov

al said:
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
 
J

Jeff Schwab

Victor said:
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.
 
B

Billy O'Connor

al said:
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?
 
A

al

Victor Bazarov said:
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?
 
V

Victor Bazarov

al said:
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, 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
 
C

Cy Edmunds

al said:
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.
 
P

P.V.S.S.VIKAS

Billy O'Connor said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top