473,387 Members | 1,757 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,387 software developers and data experts.

polymorphism with reference and pointer object

Hi I noticed that when I make a function with a base class parameter
that is a reference
than at run-time the compiler calls a base class function also if I
have passed a derived object.

But if I make that function with a pointer to a base class and then I
pass the derived class
at run-time the compiler calls a derived class function thanks to
dynamic binding.

Now why this difference if a reference is like a pointer and it
contains also an address of????

Thanks!

How is hard to learn C++ ........................

Apr 6 '07 #1
3 3432
josh wrote:
Hi I noticed that when I make a function with a base class parameter
that is a reference than at run-time the compiler calls a base class
function also if I have passed a derived object.
Yes, if it's not virtual.
But if I make that function with a pointer to a base class and then I
pass the derived class at run-time the compiler calls a derived class
function thanks to dynamic binding.
Yes, if it's virtual.
Now why this difference if a reference is like a pointer and it
contains also an address of????
There is no difference. If the function is virtual in the base class,
dynamic binding happens, no matter whether you call it through a pointer or
a reference to the object. If the function is not virtual, no dynamic
binding happens.

Apr 6 '07 #2
"josh" <xd********@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
Hi I noticed that when I make a function with a base class parameter
that is a reference
than at run-time the compiler calls a base class function also if I
have passed a derived object.

But if I make that function with a pointer to a base class and then I
pass the derived class
at run-time the compiler calls a derived class function thanks to
dynamic binding.

Now why this difference if a reference is like a pointer and it
contains also an address of????

Thanks!

How is hard to learn C++ ........................
Plese explain. The output of the following program for me is:
Derived
Derived
Derived

What is the output for you?

#include <iostream>
#include <string>

class Base
{
public:
virtual ~Base() {}
virtual void Foo() { std::cout << "Base\n"; }
};

class Derived : public Base
{
public:
virtual void Foo() { std::cout << "Derived\n"; }

};

void Foo( Base* b )
{
b->Foo();
}

void Bar( Base& b )
{
b.Foo();
}

int main()
{

Derived* dp = new Derived();
Derived d;

Foo( dp );
Foo( &d );
// Bar( dp ); // Won't Compile
// error C2664: 'Bar' : cannot convert parameter 1 from 'Derived *' to
'Base &'
Bar( d );

std::string wait;
std::getline( std::cin, wait );
}
Apr 6 '07 #3
On 6 Apr, 11:37, "Jim Langston" <tazmas...@rocketmail.comwrote:
"josh" <xdevel1...@gmail.comwrote in message

news:11**********************@n76g2000hsh.googlegr oups.com...
Hi I noticed that when I make a function with a base class parameter
that is a reference
than at run-time the compiler calls a base class function also if I
have passed a derived object.
But if I make that function with a pointer to a base class and then I
pass the derived class
at run-time the compiler calls a derived class function thanks to
dynamic binding.
Now why this difference if a reference is like a pointer and it
contains also an address of????
Thanks!
How is hard to learn C++ ........................

Plese explain. The output of the following program for me is:
Derived
Derived
Derived

What is the output for you?

#include <iostream>
#include <string>

class Base
{
public:
virtual ~Base() {}
virtual void Foo() { std::cout << "Base\n"; }

};

class Derived : public Base
{
public:
virtual void Foo() { std::cout << "Derived\n"; }

};

void Foo( Base* b )
{
b->Foo();

}

void Bar( Base& b )
{
b.Foo();

}

int main()
{

Derived* dp = new Derived();
Derived d;

Foo( dp );
Foo( &d );
// Bar( dp ); // Won't Compile
// error C2664: 'Bar' : cannot convert parameter 1 from 'Derived *' to
'Base &'
Bar( d );

std::string wait;
std::getline( std::cin, wait );

}
Damn! in my code I have two friend functions with overloaded operators
<<
in which I have a derived class parameter. Than I make
out << static_cast<Base Class>(parameter) and than is called the <<
base class
operator. I have forgotten that friend functions cannot be virtual!

Excuse me!
Apr 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: The Directive | last post by:
I can't understand why in this folling example the message "Base.Draw()" is printed. Shouldn't "Derive.Draw" be printed because of polymorphism? How can I rewrite this example using a vector to...
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...
21
by: ravinderthakur | last post by:
hi experts, just out of curosity why polymorphism is allowed to work only with pointers and references to the objects and not with the objects itself. what are the reasons behind...
12
by: ex_ottoyuhr | last post by:
I have a situation more or less as follows, and it's causing me no end of trouble; I'd appreciate anyone's advice on the matter... Given these classes: class BedrockCitizen { ... }; class...
4
by: arunvnk | last post by:
It is not quite clear why the compiler uses rntime polymorphism when the virtual keyword is specified. Since the virtual Keyword is specified,why can't the compiler decide at the runtime...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
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...
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...
12
by: feel | last post by:
Hi,All I am sure it's an old question. But I just find a interesting design about this: Polymorphism without virtual function in a C++ class. My solution is for some special case, trust me, very...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.