"josh" <xdevel1...@gmail.com> wrote in message
news:1175851680.711150.162570@n76g2000hsh.googlegroups.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 );
}
in which I have a derived class parameter. Than I make
operator. I have forgotten that friend functions cannot be virtual!