Reputation: 34728
A derived class object can be assigned to a base class object in C++.
Derived d;
Base b = d; // It's Ok
But why can't a base class object be assigned to a derived class object?
Base b;
Derived d = b; //Not Ok. Compiler give an error
Edit:
Sorry, but this question was actually asked durring an interview.
Upvotes: 10
Views: 4676
Reputation: 1387
If there is a public inheritance b/w Base and Derived class , then that is ‘is a’ relationship .
And in “is a” Relationship Derived is a Base .
One of the most importance point here is that “is a Relation ” is not bi-directional.
I.e. Derived is a Base But Base is not Derived.
Let say we have two classes Shape and Circle.
Shape is a Base and Circle is publically Inherited from Shape.
so, Circle is a Shape But Shape is not Circle
//main.cpp
#include <iostream>
#include <string>
using namespace std;
class Shape
{
private:
int x;
public:
Shape(int i):x{i}
{
cout << "Base constructor called " << endl;
}
};
class Circle : public Shape
{
private :
string color;
public :
Circle(int radius) : Shape{ radius }
{
cout << "Derived constructor called " << endl;
}
Circle(int radius, string color) : Shape{ radius }, color{ color }
{
cout << "Derived constructor called " << endl;
}
};
int main()
{
//This is valid . since a circle is a shape
Circle s(1);
Shape a = s;
return 0;
}
But you can't do this since a Shape is not a circle And inheritance is not Bi Directional here
Shape s(1);
Circle a = s;
If you do this you will get a compiler error
no suitable user-defined conversion
from "Shape" to "Circle" exists
Upvotes: 2
Reputation: 63174
A derived object is a base object, with additional information.
You can initialize a complete base object from the base part of a derived object, no problem.
But if you want to construct a derived object from just a base object, what should the additional information be initialized with?
If you want to provide defaults for that additional information, you can do so by declaring a Derived(Base const &)
constructor. But since it does not work in the general case, it isn't done for you.
Upvotes: 10
Reputation: 413
How about using a temporary reference?
Base b;
Derived d;
Base & d_ref = d;
d_ref = b;
Upvotes: -2
Reputation: 234885
In general a C++ compiler will disallow the assignment of a object of a base class to a derived one as, in a sense, the derived class is a superset of the base class: i.e. it wouldn't know how to to deal with any members that are specific to the derived class.
That said, you can handcode such a possibility by an appropriate overload of the assignment operator and an appropriate constructor in the derived class.
Aside from perhaps overcomplicating the language, I don't see why a trivially copyable base class instance could not be assigned to a derived class that contains no additional members. But this is not implemented in any C++ standard at the time of my writing. Furthermore, to my mind at least, the consequence of having any uninitialised derived class members and bypassed derived class constructors doesn't require materially more consideration on the part of a programmer than the perils of object slicing if a derived class instance is assigned to a base class! In other words, I don't think the hackneyed retort "because it makes no sense" makes much sense in itself.
Reference: http://en.cppreference.com/w/cpp/types/is_trivially_copyable
Upvotes: 3
Reputation: 409482
Inheritance is an "is-a" relationship, but it's one-way only.
If you have e.g.
struct Base { /* ... */ };
struct Derived : Base { /* ... */ };
Then Derived
is a Base
, but Base
is not a Derived
.
That's why you can assign or initialize a base-class instance with a derived object (but beware of object slicing), but not the other way around.
Upvotes: 10