#include "stdafx.h"
#include<iostream>


using namespace std;

class Base
{
public:
	virtual void fun()
	{
		cout<<"base"<<endl;
	}
};


class Der:public Base
{
public:
	void fun()
	{
		cout<<"Der"<<endl;
	}
};


void func(Base b)
{
	b.fun(); 
}
int _tmain(int argc, _TCHAR* argv[])
{
  Base b;
  Der d;
     b.fun();
	 d.fun();

	 func(b);
	 func(d);

	system("pause");
	return 0;
}

o/p:
base
Der
base
base

void func(Base *b)
{
	b->fun(); 


}

int main()
{
 Base b;
  Der d;
     b.fun();
     d.fun();

	 func(&b);
	 func(&d);

	system("pause");
	return 0;
}

o/p:
base
Der
base
Der

can anyone please explain how virtual mechanism works fine with pointer but not with object !!
Correct me if i am wrong !

TIA

Recommended Answers

All 3 Replies

When you pass the object by value (not pointer or reference), a copy of the object is made and used as the parameter. Because the parameter is of type Base, the new object (the copy of the passed object) is of type Base, regardless of the type of the object that is given as parameter to the function call, as long as there is a way to make an object of type Base from the object that is passed to the function call. By default, the compiler generates a copy-constructor for Base that takes an object of type Base by reference (to which an object of type Der can be implicitly cast to).

In summary, here are the cases:

#include<iostream>

class Base {
  public:
    virtual void fun()
    {
      std::cout << "base" << std::endl;
    };
};

class Der : public Base {
  public:
    void fun() {
      std::cout << "Der" << std::endl;
    };
};

void func_by_value(Base b) {
  b.fun(); 
};

void func_by_ref(Base& b) {
  b.fun();
};

void func_by_ptr(Base* b) {
  b->fun();
};

int main() {
  Base b;
  Der d;
  b.fun();          //output: "base"
  d.fun();          //output: "Der"

  func_by_value(b); //output: "base"
  func_by_value(d); //output: "base"
  func_by_ref(d);   //output: "Der"
  func_by_ptr(&d);  //output: "Der"

  return 0;
};

If you want the equivalent of passing-by-value, i.e. you don't want to allow modification of the original object, you can pass by const reference.

From the standard:

11.2. A pointer to a derived class can be implicitly converted to a pointer to an accessible unambiguous base class.
(4.10). An lvalue of a derived class type can be bound to a reference to an accessible unambiguous base class.

Two pointers of a base class and a derived class will always share the same size but two objects of different types won't even if one's type is derived from the other and since you can't mix types this the provided alternative.

Thanks MIKE !!
useful reply

When you pass the object by value (not pointer or reference), a copy of the object is made and used as the parameter. Because the parameter is of type Base, the new object (the copy of the passed object) is of type Base, regardless of the type of the object that is given as parameter to the function call, as long as there is a way to make an object of type Base from the object that is passed to the function call. By default, the compiler generates a copy-constructor for Base that takes an object of type Base by reference (to which an object of type Der can be implicitly cast to).

In summary, here are the cases:

#include<iostream>

class Base {
  public:
    virtual void fun()
    {
      std::cout << "base" << std::endl;
    };
};

class Der : public Base {
  public:
    void fun() {
      std::cout << "Der" << std::endl;
    };
};

void func_by_value(Base b) {
  b.fun(); 
};

void func_by_ref(Base& b) {
  b.fun();
};

void func_by_ptr(Base* b) {
  b->fun();
};

int main() {
  Base b;
  Der d;
  b.fun();          //output: "base"
  d.fun();          //output: "Der"

  func_by_value(b); //output: "base"
  func_by_value(d); //output: "base"
  func_by_ref(d);   //output: "Der"
  func_by_ptr(&d);  //output: "Der"

  return 0;
};

If you want the equivalent of passing-by-value, i.e. you don't want to allow modification of the original object, you can pass by const reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.