473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

polymorphism - virtual function

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
7 1394
"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
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
"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
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
"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: john sun | last post by:
Hello, I am not newbie C++ developer infact :). But till recently I would like dig up more about C++. I know those OO details. But when people talking OO they focused on the polymorphism, and...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
35
by: JKop | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en- us/vccore98/HTML/_core_using_strict_type_checking.asp Pay particular attention to: The types WPARAM, LPARAM, LRESULT, and void *...
7
by: Samee Zahur | last post by:
Hello, The other day I was rather shocked to find that I couldn't find a good use for runtime polymorphism! Let me explain this a bit further before you get shocked. Any function that I could...
8
by: Locia | last post by:
What are the techniques used to implement parametric polymorphism in C++? A Polymorphic method can be virtual?
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
1
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.