I received homework to make program without casting using constructors so this is my code, I have two classes:
class Base {
protected:
int var;
public:
Base(int var = 0);
Base(const Base&);
Base& operator=(const Base&);
virtual ~Base(){};
virtual void foo();
void foo() const;
operator int();
};
class Derived: public Base {
public:
Derived(int var): Base(var){};
Derived(const Base&);
Derived& Derived::operator=(const Base& base);
~Derived(){};
virtual void foo();
};
here two of my functions of Derived:
Derived::Derived(const Base& base){
if (this != &base){
var=base.var;
}
}
Derived& Derived::operator=(const Base& base){
if (this != &base){
var=base.var;
}
return *this;
}
but I have an error within context
when I call these rows
Base base(5);
Base *pderived = new Derived(base); //this row works perfectly
Derived derived = *pderived; // I think the problem is here
thanks for any help