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
You can only access protected members from another object if that object is of the same type as the object that is trying to access it. In your example the constructor and assignment operator both take in a const Base& so there is no guarantee that the actual object will be of type Derived.
There is an error (VS2010)
error C2248: 'Base::var' : cannot access protected member declared in class 'Base'
at the line
var=base.var;
Derived needs to delegate copying of Base members to Base::operator=, instead of trying to put its grubby little hands on the protected members of another object.
Related
Suppose I'm dealing with the following header file(for experimental purposes):
class Base{
protected:
Data& data1;
public:
Base(Data& data1);
virtual Base* clone() const =0;
virtual ~Base();
virtual void do() = 0;
};
class Derived: public Base{
private:
const int data2;
public:
Derived(int data2, Data& data1);
virtual Derived* clone() const;
~Derived();
}
I tried to implement these classes which are fairly simple:
Base:
Base::Base(Data& data1): data1{data1} {}
virtual Base::~Base() = default;
Derived:
Derived::Derived(int data2, Data& data1): Base(data1), data2{data2} {}
virtual Derived* Derived::clone() const {
return new Derived(*this);
}
virtual void Derived::do() {
return;
}
So far so good, but I'm having a couple of issues:
Say I wanted to implement a copy constructor and a copy assignment operator for this hierarchy,
should I do it in both classes? If so, is this a good way to do it? :
Base:
Base::Base(const Base& other): Base(other.data1) {}
Base& Base::operator=(const Base& rhs){
if(this == &rhs)
return *this;
data1 = rhs.data1; // is this possible? since data1 is a reference (cannot be reassigned)
return *this;
}
and which is a proper way to do so for derived?:
Derived:
Derived::Derived(const Derived& other): Base(other.data1), data2{other.data2} {}
OR
Derived::Derived(const Derived& other): Derived(other.data2, other.data2) {}
Derived& Derived::operator=(const Derived& rhs){
if(this == &rhs)
return *this;
data1 = rhs.data1;
data2 = rhs.data2; // data2 is a const variable so this is probably invalid too
return *this;
}
Once I implement a copy constructor for derived, I get an 'Endless loop' warning when returning from clone() method. Why is that?
Is there a proper way to implement rule of 3/ rule of 5 for classes of such hierarchy?
Thanks ahead.
Say I wanted to implement a copy constructor and a copy assignment operator for this hierarchy, should I do it in both classes? If so, is this a good way to do it?
Yes, you have to provide copy constructor/assignment.
Best way, when possible/correct, is to use default.
For assignment, your issue is unrelated to hierarchy, but related to member you use:
Reference cannot be rebound. const member cannot be changed.
So default assignment cannot be generated, mark them as deleted.
class Base
{
// ...
Base(const Base&) = default;
Base& operator=(const Base&) = delete;
};
class Derived
{
// ...
Derived(const Derived&) = default;
Derived& operator=(const Derived&) = delete;
};
Once I implement a copy constructor for derived, I get an 'Endless loop' warning when returning from clone() method. Why is that?
Cannot reproduce from comment
Is there a proper way to implement rule of 3/ rule of 5 for classes of such hierarchy?
Hierarchy is unrelated to rule of 5/3/0.
Just respect rule of 5/3/0 for each class.
There is well-known clone idiom for copying Derived objects via pointer to Base class.
class Base{
int b;
public:
virtual unique_ptr<Base> clone() const = 0;
virtual ~Base() = default;
};
class Derived : public Base {
int d;
public:
virtual unique_ptr<Base> clone() const override {
return std::make_unique<Derived>(*this);
}
}
However, I can't find clear instructions how to define copy constructors and assignments in this case. This is how I suppose it should be done in Base class:
class Base {
protected:
Base(const Base&) = default;
private:
Base& operator=(const Base&) = delete;
}
Is it necessary (in order to avoid potential slices)? Is it right way to do it? Does it suffice or should I add such declarations to Derived class as well?
As derived classes use the copy constructor to create clones, you may like to make the copy constructors non-public to avoid accidental slicing but accessible to derived classes.
protected fills this requirement. It needs to be applied to each class' copy constructor because the compiler-generated copy constructor is public. It also makes sense to apply the same treatment to the assignment operator.
That also prevents std::make_unique from accessing the copy constructor though:
class A
{
protected:
A(A const&) = default;
A& operator=(A const&) = default;
public:
A();
virtual std::unique_ptr<A> clone() const = 0;
};
class B : public A
{
protected:
B(B const&) = default;
B& operator=(B const&) = default;
public:
B();
std::unique_ptr<A> clone() const override {
return std::unique_ptr<A>(new B(*this));
}
};
Deleting the copy assignment operator is probably a good idea, unless you need it.
Deleting operator=(const Base&) in Base is enough, as the implicitly declared copy assignment operator is defined as deleted for a derived class if the base class has no copy assignment operator (see cppreference.com).
If you really want copy assignment you can make the copy assignment operator virtual, and carefully implement the correct behaviour in the derived classes by
calling Base::operator= to assign the Base class members, and
assigning the members of the derived class, using dynamic_cast to ensure that the argument is of the correct type .
If done correctly, this avoids object slicing and retains the correct type.
An example (with copy constructor details omitted):
struct Point {
virtual Point& operator=(const Point& p) =default;
int x;
int y;
};
struct Point3d :public Point{
virtual Point3d& operator=(const Point& p);
int z;
};
Point3d& Point3d::operator=(const Point& p)
{
Point::operator=(p);
auto p3d = dynamic_cast<const Point3d*>(&p);
if(p3d){
z = p3d->z;
} else {
z = 0;
}
return *this;
}
I have the following MWE code:
#include <algorithm>
class Base{
public:
int baseMember;
friend void swap(Base& in, Base& out)
{
using std::swap;
swap(in.baseMember, out.baseMember);
}
virtual Base& operator=(Base obj)
{
swap(*this, obj);
return *this;
}
Base() : baseMember(1)
{
}
};
class Derived : public Base
{
public:
int derivedMember;
friend void swap(Derived& in, Derived& out)
{
using std::swap;
swap(in.derivedMember, out.derivedMember);
swap(static_cast<Base&>(in), static_cast<Base&>(out));
}
virtual Base& operator=(Base obj)
{
swap(*this, static_cast<Derived&>(obj));
return *this;
}
Derived() : Base(), derivedMember(2)
{
}
};
int main()
{
Base *b1 = new Derived();
Base *b2 = new Derived();
*b1 = *b2;
delete b1;
delete b2;
}
I have two Base pointers pointing to Derived data. I then do an assignment of the contents of the pointers. Since the Base class' assignment operator is virtual, polymorphism kicks in and the assignment operator of the Derived class (with the same signature) is called instead.
However, the static_cast to transform the source into a Derived object fails. Or, well, it successfully transforms it into a Derived object, but its derivedMember is garbage (after being initially set in the constructor).
How can this be avoided? How can an assignment between the Derived contents of two Base pointers be done?
Your code has a typo and inherently unsafe behavior. The typo is here:
virtual Base& operator=(Base obj) // <-- should be base&
{
swap(*this, static_cast<Derived&>(obj));
return *this;
}
If you fix this, your code should work in the simple example provided. But it would still fail at large, because how would you guarantee the argument passed to operator= will be in fact of Derived type?
Passing an argument by value slices that argument. So by the time your operator= function is called, you have an actual Base, not a Derived.
I'm afraid you'll need to overload your assignment for lvalues and rvalues.
virtual Base& operator=(const Base&);
virtual Base& operator=(Base&&);
This is a recurrent problem once again. Someone know a easy way to do that? Imagine I have the following:
class Base
{
public:
...
Base property(const std::string& name)=0;
};
class Derived:public Base
{
public:
Derived();
Derived(const Derived&& val);
Base property(const std::string& name)
{
Derived z;
return z;
}
}
There is a way for the Derived::property return being (internally) a Derived copy instead of only Base part copy, and with the Derived move constructor invoked?
May be a stupid question, but really I dont find solution. Why copy constructors on return dont copy the specialized class?
Thanks you!
You can't do this.
Returning by value conceptually (ignoring RVO and move semantics) means making a copy of whatever you return by using the copy constructor of the type which the function is declared to return. If you return a Derived, a copy of type Base will be made and you'll lose the Derived part of the object. This is known as slicing.
If you want to return a Derived object as a Base, you'll need to use pointers.
The only aproximation I can find for who search something similar (related with X3liF, TartanLlama and other responses)
#define overridable(T) ovr<T>
#define return_overload_allowed(TYPE) friend struct ovr<TYPE>; virtual void* clone() const
#define return_overload_basic_allowed(TYPE) friend struct ovr<TYPE>; virtual void* clone() const{return new TYPE(*this);}
template<typename T> struct ovr
{
T* _obj;
ovr(const T& t)
: _obj(reinterpret_cast<T*>(t.clone()))
{;}
ovr(ovr<T>&& v)
: _obj(v._obj)
{
v._obj=nullptr;
}
operator T&()
{
return *_obj;
}
virtual ~ovr()
{
delete _obj;
}
};
class BASE
{
return_overload_basic_allowed(BASE);
public:
virtual overridable(BASE) method1();
virtual ~BASE();
};
class DERIVED: public BASE
{
return_overload_basic_allowed(DERIVED);
public:
virtual overridable(BASE) method1()
{
DERIVED a;
return a;
}
virtual ~DERIVED();
};
DERIVED a;
auto x = a.method1();
BASE& really_derived = x;
This compiles fine. But don't meet practical and smart requiriments... :(
#include<iostream>
using namespace std;
class Something
{
public:
int j;
Something():j(20) {cout<<"Something initialized. j="<<j<<endl;}
};
class Base
{
private:
Base(const Base&) {}
public:
Base() {}
virtual Base *clone() { return new Base(*this); }
virtual void ID() { cout<<"BASE"<<endl; }
};
class Derived : public Base
{
private:
int id;
Something *s;
Derived(const Derived&) {}
public:
Derived():id(10) {cout<<"Called constructor and allocated id"<<endl;s=new Something();}
~Derived() {delete s;}
virtual Base *clone() { return new Derived(*this); }
virtual void ID() { cout<<"DERIVED id="<<id<<endl; }
void assignID(int i) {id=i;}
};
int main()
{
Base* b=new Derived();
b->ID();
Base* c=b->clone();
c->ID();
}//main
On running:
Called constructor and allocated id
Something initialized. j=20
DERIVED id=10
DERIVED id=0
My question is related to this, this and this post.
In the first link, Space_C0wb0y says
"Since the clone-method is a method of
the actual class of the object, it can
also create a deep-copy. It can access
all members of the class it belongs
to, so no problems there."
I don't understand how a deep copy can happen. In the program above, not even a shallow copy is happening. I need it to work even if the Base class is an abstract class. How can I do a deep copy here? Help please?
Well, your copy constructor does nothing, so your clone method does nothing in the way of copying.
See line Derived(const Derived&) {}
EDIT: if you add code to copy by assignment all members of Derived, it will become a shallow copy. If you also copy (by making a new instance) your instance of Something, it will become a deep copy.