I am trying to implement an operator= in C++ for an object which has as a member a pointer to a user defined type which also has dynamic memory allocated in it.
So given the code below, how would on implement a correct operator= for B? What I am after is how is the dynamic memory in A copied to the new B object?
Any help would be much appreciated.
Thank you
class A
{
int a;
int* b;
public:
A()
{
a = 1;
b = new int [20];
}
};
class B
{
A* a;
public:
B()
{
a = new A;
}
}
For starters you should define at least copy constructor, copy assignment operator and destructor for the class A. Then it is simple to define the copy assignment operator for the class B.
For example
#include <iostream>
#include <algorithm>
class A
{
static const size_t N = 20;
int a;
int* b;
public:
A()
{
a = 1;
b = new int [N]();
}
A( const A &a ) : a( a.a ), b( new int [N] )
{
std::copy( a.b, a.b + N, this->b );
}
A & operator =( const A &a )
{
if ( &a != this )
{
this->a = a.a;
int *p = new int[N];
std::copy( a.b, a.b + N, p );
delete [] this->b;
this->b = p;
}
return *this;
}
~A()
{
delete []b;
}
};
class B
{
A* a;
public:
B() : a( new A )
{
}
// copy constructor
~B()
{
delete a;
}
B & operator =( const B &b )
{
if ( this != &b )
{
*this->a = *b.a;
}
return *this;
}
};
int main()
{
B b1;
B b2;
b1 = b2;
}
Pay attention to that in the copy assignment operator at first created a new array before deleting the old one. This allows to keep the stable state of the assignable object if an exception will occur.
At very first: Have a look at the rule of three, it is an absolute must go in given case. Consider, too, the rule of five, while not mandatory, you'll leave out a great optimisation opportunity...
The destructor would now delete[] the array (leaving this part to you...), a copy constructor would then do exactly that: (deep) copy the data:
A::A(A const& other)
: a(other.a), b(new int[20]) // assuming you have a fixed size for those arrays;
// better: introduce a constant for to avoid magic
// numbers in code!
{
// you created a new array, but yet need to fill it with the others value
std::copy(other.b, other.b + 20, b);
}
OK, first step. Using the copy and swap idiom, the assignment operator gets pretty simple:
A& operator=(A other) // YES, no reference! This will invoke the copy (or move!)
// constructor of your class!
{
swap(*this, other); // you'll need to implement it yet!
return *this;
// at this point, the destructor of other will clean up data that was potentially
// contained in *this before...
}
Finally the move constructor:
A::A(A&& other)
: a(0), b(nullptr)
{
swap(*this, other);
// again swapping??? well, sure, you want the data from other to be contained
// in *this, and we want to leave other in some kind of valid state, which the
// nullptr is fine for (it's fine to delete[] a null pointer, so you don't even
// need to check in the destructor...)
}
And now up to you: class B analogously...
Side note: you get away a bit cheaper by use of a smart pointer (std::unique_ptr), it will allow you to default destructor and move constructor + assignment operator, solely copy constructor and assignment operator need to be implemented explicitly (std::unique_ptr is not copiable...).
Related
What exactly happens at 1 & 2 internally , when we have assigned by return reference?
Class A
{
A& operator= (const A &ax)
{
return *this;
}
}
int main()
{
A a;
A b;
b = a; // -------------- 1
A c = a; // ----------- 2
}
// ---------------------------------------------------------------
What difference will happen in below (assigned returned value)? :
Class A
{
A operator= (const A &ax)
{
return *this;
}
}
int main()
{
A a;
A b;
b = a; // -------------- 3
A c = a; // ----------- 4
}
There is also principal mistake, because operator= made private and code is ill-formed, but this would be corrected by adding public: access modifiers.
operator= is invoked from object stored to b variable, so this variant of assignment should not perform assignment at all.
#include <iostream>
class A
{
public:
float a1;
A& operator= (const A &ax)
{
return *this;
}
};
int main()
{
A a;
a.a1 = 3;
A b;
b.a1 = 5;
b = a; // -------------- 1
std::cout << b.a1; // outputs 5
A c = a;
std::cout << c.a1; // outputs 3
}
The line
A c = a;
is called initialization and doesn't use operator=, instead it uses (generated by compiler) copy constructor, which does a shallow copy of object.
Your mistake is that member operator= meant to assign its argument's value to this. operator= return result of assignment, which can be used for chaining: a=b=c;
A& A::operator= (const A &right)
is equivalent of
A& operator= (A &left, const A &right)
I am working on the following code
class base
{
private:
char* mycharpointer;
std::string mystring;
public:
base() : mycharpointer(NULL) {/*default constructor*/}
//Copy Constructor
base(const base& rhs){
if(mycharpointer != NULL) ---> Why is this condition true ?
{
mycharpointer = new char[ strlen(rhs.mycharpointer + 1)];
strcpy(this->mycharpointer,rhs.mycharpointer);
}
mystring = rhs.mystring;
}
base operator=(base& b)
{
if(this == &b)
return *this;
base temp(b);
temp.swap(*this);
return *this;
}
//Swap operation
void swap(base& lhs) {
std::swap(lhs.mycharpointer,this->mycharpointer);
std::swap(lhs.mystring,this->mystring);
}
//Destructor
virtual ~base(){
if(mycharpointer)
delete[] mycharpointer;
}
};
class der : public base
{
public:
char* mycharpointer_der;
std::string mystring_der;
foo* f;
public:
der():mycharpointer_der(NULL)
{
}
der(const der& rhs) : base(rhs)
{
if(mycharpointer_der)
{
mycharpointer_der = new char[ strlen(rhs.mycharpointer_der + 1)];
strcpy(this->mycharpointer_der,rhs.mycharpointer_der);
}
mystring_der = rhs.mystring_der;
f = new foo(*rhs.f);
}
der& operator=(der& d)
{
if(this == &d) //Make sure its not the same class
return *this;
base::operator= (d);
der temp(d);
temp.swap(*this);
return *this;
}
//Swap operation
void swap(der& lhs) {
std::swap(lhs.mycharpointer_der,this->mycharpointer_der);
std::swap(lhs.mystring_der,this->mystring_der);
}
virtual ~der(){
if(mycharpointer_der) //Necessary check as to make sure you are not deleting a NULL address otherwise exception thrown.
delete[] mycharpointer_der;
}
};
int main()
{
der d;
d.mycharpointer_der = "Hello World";
d.mystring_der = "Hello String";
der b;
b = d;
}
Now in the above code the copy assignment operator of d is called. which in return calls the copy assignment operator of the base class. In the copy assignment operator of the base class the copy constructor of the base class is called. My question is why is the condition
if(mycharpointer != NULL)
in the base class turning out as true ? Even when I have explicitly assigned it a NULL in the initialization list of the base class.
That check is ridiculous. At the point of construction, when we get into the body, mycharpointer is default-initialized and will contain some garbage value which may be 0 but likely won't be.
That said, what happens if rhs.mycharpointer is NULL? Then the strlen call would fail. That's the charpointer whose value you need to be checking:
base(const base& rhs)
{
if (rhs.mycharpointer) {
mycharpointer = new char[ strlen(rhs.mycharpointer) + 1 ];
// outside the parens ^^^^
strcpy(this->mycharpointer,rhs.mycharpointer);
}
else {
mycharpointer = NULL;
}
mystring = rhs.mystring;
}
Or since you're already using string, we could keep using string for mycharpointer too. That has the added benefit of us not even having to write the copy constructor which, as you can see, can be error prone:
base(const base& ) = default;
C++ compiler only make sure the global variable and static variables will be initialized, so In this case, the mycharpointer may actually points to some useless garbage(dangling pointer)
base(const base& rhs){
if(mycharpointer != NULL) ---> Why is this condition true ?
{
mycharpointer = new char[ strlen(rhs.mycharpointer + 1)];
strcpy(this->mycharpointer,rhs.mycharpointer);
}
mystring = rhs.mystring;
}
as mycharpointer actually point to the data allocated on the heap, so if you want to rellocate it, you need to release existing data first.
something like:
if ( mycharpointer) {
delete [] mycharpointer;
}
Apparently you're expecting the default constructor to run before the copy constructor does.
But it won't.
In a copy constructor of a struct/class, how can I avoid copying all the basic (int, double, etc.) members one by one if the intention is to copy a pointer successfully? Is it possible to extend the default copy constructor in this sense?
struct Type
{
int a;
double b;
bool c;
// ... a lot of basic members
int* p;
Type()
{
p = new int;
*p = 0;
}
Type (const Type& t)
{
// how to avoid copying these members one by one
this.a = t.a;
this.b = t.b;
this.c = t.c;
// but only add this portion
this.p = new int;
*this.p = *t.p;
}
};
Create an RAII wrapper for the int * data member that allows copying/moving.
struct DynInt
{
std::unique_ptr<int> p;
DynInt() : DynInt(0) {}
explicit DynInt(int i) : p(new int(i)) {}
DynInt(DynInt const &other) : p(new int(*other.p)) {}
DynInt& operator=(DynInt const& other)
{
*p = *other.p;
return *this;
}
DynInt(DynInt&&) = default;
DynInt& operator=(DynInt&&) = default;
// maybe define operator* to allow direct access to *p
};
Then declare your class as
struct Type
{
int a;
double b;
bool c;
// ... a lot of basic members
DynInt p;
};
Now, the implicitly generated copy constructor will do the right thing.
I would like to further exhaust this topic.
Assume that I have something like:
class MyClass
{
public:
MyClass(int N)
{
data_ptr = new float[N];
};
float* dat_ptr;
// ... clever operator definition here ...
};
So I would like to be able to simply write:
MyClass a(4);
MyClass b(4);
MyClass c(4);
// modify b.data_ptr and c.data_ptr ....
// Use "clever operator"
a = b + c;
Where the operator would do a.data_ptr[i] = b.data_ptr[i] + c.data_ptr[i] for i=0:(N-1) ...
Hence no extra copies of the data are created and we are neatly using the preallocated buffers.
Is this possible? If so, please provide me with som insights as to how it would be done.
Thanks!
You can, if you use move semantics from C++11.
class MyClass
{
public:
MyClass(int N)
{
data_ptr = new float[N];
n = N;
}
MyClass(MyClass && rhs)
{
data_ptr = rhs.data_ptr;
n = rhs.n;
rhs.data_ptr = nullptr;
}
// dtor, copy-ctor etc.
int n;
float * dat_ptr;
};
MyClass operator + (const MyClass & left, const MyClass & right)
{
MyClass result(left.n);
// Implement addition
}
// Note: no error-checking
This way a temporary object will be created, but the internal data will not be unnecessarily copied.
Read more about the move semantics.
It is not possible; Before a is assigned to, a temporary object will be created as a result of calling operator + (b, c); This operator should return the created instance, that should then be assigned to a; the created instance is always created by b + c.
What is possible though is to define += as a member operator and say:
b += c;
This would modify the value of b without creating extra copies.
Edit: I have reconsidered :)
You definitely can do it, by abstracting operations as lazy evaluation objects.
Here is an example:
class MyClass; // fwd. declaration of your class
struct LazySum
{
LazySum(const MyClass& a, const MyClass& b)
: x(a), y(b) {}
float operator[](int i) { return x[i] + y[i]; }
const MyClass& x;
const MyClass& y;
};
class MyClass
{
public:
MyClass(int N)
{
data_ptr = new float[n = N];
};
int n; // this shouldn't be public
float* dat_ptr; // nor this, but I went with your code
// ... clever operator definition here ...
MyClass& operator=(const LazySum& terms)
{
// ignore case when n != x.n or n != y.n
// because not the point of the example
// (and I'm lazy)
// sum evaluation occurs here
// with no new allocations
for(int i = 0; i < n; ++i)
data_ptr[i] = terms[i];
return *this;
}
};
LazySum operator=(const MyClass& x, const MyClass& y)
{
return LazySum(x, y); // LazySum is a couple of references in size
}
void client_code_using_clever_op()
{
MyClass a(4);
MyClass b(4);
MyClass c(4);
// modify b.data_ptr and c.data_ptr ....
// Use "clever operator"
a = b + c; // actual sum performed when operator = is executed
}
The idea is to store the terms, and perform late evaluation on the terms.
Points of improvement:
inject a functor in the construction of LazySum to make it become LazyOp (the functor would decide what the op is); Implement other binary operators on MyClass in terms of it.
use RAII in MyClass.
when you need to implement lazy evaluation operators on another type (e.g. some MyOtherClass) consider implementing LazyOp as a template on the terms and functor type.
this does not support more complex expressions without some extra work:
MyClass a(4), b(4), c(4), d(4);
d = (a + b) + c; // error
This example will not work because it would require an operator+(const LazySum&, const MyClass&);;
As Spook explained, yes it is possible. Just for fun I wrote a full example that you can compile and run. If a copy was to be created, you would get a message in the output. I tried this example in Visual Studio 2012 and runs fine.
class MyClass
{
private:
float *data_ptr;
std::size_t size;
public:
MyClass(std::size_t N = 0) :
size(N),
data_ptr(N ? new float[N]() : nullptr)
{}
MyClass(const MyClass& other) :
size(other.size),
data_ptr(other.size ? new float[other.size]() : nullptr)
{
std::copy(other.data_ptr, other.data_ptr + size, data_ptr);
std::cout << "Copy!" << std::endl;
}
MyClass(MyClass&& other)
{
size = 0;
data_ptr = nullptr;
swap(*this, other);
}
~MyClass()
{
delete[] data_ptr;
}
MyClass& operator=(MyClass other)
{
swap(*this, other);
return *this;
}
friend MyClass operator+(MyClass& first, MyClass& second)
{
MyClass result(std::min(first.size, second.size));
for (std::size_t i=0; i < result.size; i++) {
result.data_ptr[i] = first.data_ptr[i] + second.data_ptr[i];
}
return result;
}
friend void swap(MyClass& first, MyClass& second)
{
std::swap(first.size, second.size);
std::swap(first.data_ptr, second.data_ptr);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
MyClass a(5);
MyClass b(5);
MyClass c(5);
a = b + c; //this should not produce an extra copy
return 0;
}
I have a class like the following:
class A {
SuperHugeClass* s;
public:
A(){ s = new SuperHugeClass(); }
};
Because SuperHugeClass takes a lot of memory, I'm fine with the shallow copying provided by the default constructor and assignment operator. However, I also don't want to leak memory, so I need to delete s, but I have to be careful about it because otherwise I'll delete it more than once.
One way of doing this is by refcounting s as follows:
class A {
int* refcount;
SuperHugeClass* s;
public:
A(){
refcount = new int(1);
s = new SuperHugeClass();
}
A(const A& other) : refcount(other.refcount), s(other.s) {
(*refcount)++;
}
~A() {
(*refcount)--;
if (!(*refcount)) {
delete refcount;
delete s;
}
}
friend void swap(const A& a, const A& aa) {
std::swap(a.refcount, aa.refcount);
std::swap(a.s, aa.s);
}
A& operator=(A other) {
swap(*this, other);
return (*this);
}
};
This is the first time I've needed to do something like this, but it seems to me that this should be pretty standard and so there should be a 'canonical' solution. Are there any other ways of doing this? Thanks!
Use std::shared_ptr
class A {
std::shared_ptr<SuperHugeClass> s;
public:
A()
: s(new SuperHugeClass())
{
}
};
and thats it. Default generated copy constructor/assignment operator/destructor do just what you need.
Use std/boost::shared_ptr instead of your ref-counted pointer.