Я получил домашнее задание по созданию программы без приведения с использованием конструкторов, так что это мой код, у меня есть два класса:
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();
};
вот две мои функции из 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;
}
но у меня появляется ошибка within context
, когда я вызываю эти строки
Base base(5);
Base *pderived = new Derived(base); //this row works perfectly
Derived derived = *pderived; // I think the problem is here
спасибо за любую помощь