polymorphism with reference and pointer object

J

josh

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++ ........................
 
R

Rolf Magnus

josh said:
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.
 
J

Jim Langston

josh said:
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 );
}
 
J

josh

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!
 

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
474,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top