Reputation: 7843
I have those two classes
///////////BASE CLASS
class Base{
public:
Base(int = 0);
~Base();
Base(Base&);
Base(Derived&); ////<-may be problem here?, note: I tried without this function
int valueOfBase();
protected:
int protectedData;
private:
int baseData;
};
/////////////DERIVED CLASS
class Derived: public Base{
public:
Derived(int);
//Derived(Derived&);
~Derived();
private:
int derivedData;
};
and here my main
int main(){
Base base(1);
Derived derived = base;
return 0;
}
I read that if my derived class doesn't have copy c'tor copy c'tor of the base will be called
but every time I receive conversion from Base to non-scalar type Derived requested
who is wrong? my compiler or my book, or I've just misunderstood? thanks in advance
Upvotes: 3
Views: 2582
Reputation: 92854
Just a hint.
Does the following code give the same error?
class base{};
class derived: public base{};
int main()
{
derived d= base();
}
Yes? Why? Because there is no conversion from the base class to the derived class. You should define this conversion if you want your code to compile.
How about adding this to the derived class ?
derived(const base &b){}
Makes sense, huh?
Upvotes: 3