Marking a member variable to not copy - c++

I have a class that contains POD members. I need to have all members copied, except one (member a in the example case). The way I'm doing it right now is as follows:
class Example
{
private:
int a = 0;
int b = 0;
int c = 0;
int d = 0;
double e = 0;
public:
Example& operator=(const Example &rhs)
{
b = rhs.b;
c = rhs.c;
d = rhs.d;
e = rhs.e;
return *this;
}
Example() {};
Example(const Example& toCopy)
{
*this = toCopy;
}
};
Is there a way to mark the variable to not copy, as doing it this way is verbose and prone to bugs if I later modify this class?

You can wrap the "odd man out", a, in a struct and define that structs behavior separately:
class Example
{
private:
CopyLess a;
int b = 0;
int c = 0;
int d = 0;
double e = 0;
struct CopyLess {
int a = 0;
CopyLess& operator=(const CopyLess&) { return *this; }
CopyLess(const CopyLess&) {}
};
};
Note that I didn't bother writing any special members for Example anymore because the defaults do what you want. Writing code this way to avoid writing special members as much as possible is called the "Rule of Zero", more information and examples: http://www.nirfriedman.com/2015/06/27/cpp-rule-of-zero/.

No, you cannot make the implicit copy assignment skip members. Defining a custom assignment operator as you have done is the way to achieve your desired behaviour.
Doing it this way is verbose and prone to bugs if I later modify this class
You can group the copied members into a single subobject. That way the custom copy assignment is not fragile to changes and verbosity remains constant in relation to number of copied members.
Bonus: You don't need to specify value initialization for the members separately, but one for the entire subobject is enough.
class Example
{
int a = 0;
struct {
int b;
int c;
int d;
double e;
} state = {};
public:
Example& operator=(const Example &rhs)
{
state = rhs.state;
return *this;
}
...
Since the members are private this won't change the existing API of the class.

Related

Keep pointers between class members stable when moving

Suppose a class C has two members, M1 and M2, where M1 holds a pointer P that refers to M2.
For every instance X of C the following invariant should hold: X.M1.P points to X.M2.
Is there an idiomatic way to implement this behaviour in C++ without having to implement a move constructor and assignment operator?
I'm asking because it seems to me that conceptually the pointer P should be relative to M1. Non-default move constructors and assignment operators seem like lots of work (especially if there are a few members) for this simple concept.
My specific use-case is similar to the example below (where set is M1, and less is M2). Perhaps my fundamental approach is flawed — is there a better way than having a pointer to Relation in Less?
using Relation = std::vector<std::vector<bool>>;
class C {
public:
C() { /* less = ...; */ }
C(C&&) = default;
C& operator=(C&&) = default;
private:
struct Less {
explicit Less(const Relation* r) : r(r) {}
bool operator()(int i, int j) { return (*r)[i][j]; }
const Relation* r;
};
Relation less;
std::set<int, Less> set{Less(&less)};
};
C a;
std::cout << a.set.key_comp().r == &a.less << std::endl; // true
C b = std::move(a);
std::cout << b.set.key_comp().r == &b.less << std::endl; // false

making public member read only

class A{
private:
int a;
public:
const int &ref = a;
};
int main() {
A obj;
obj.a = 20; // error cause private
obj.ref = 30; // not private but const so ERROR
return 0;
}
I'm trying to make a member variable accessible but read only through the interface. Currently I've tried this approach and it seems to compile fine. I made a const reference to my original variable int a and made it public. Is there anything that's wrong with this practice that I might be missing out? Or is this example safe and sound to use for practical purposes?
Nothing wrong with providing a member function with const correctness applied (and I've used that too and intend to do so always), but I'm asking is there any thing wrong with this way if I have to provide a variable that is only read-only.
Thankyou :)
class A{
private:
int a;
public:
const int &ref = a;
};
is there any thing wrong with this way if I have to provide a variable that is only read-only
There are at least a couple drawbacks with this design decision for class A.
1: Class Size
Also as Dieter Lücking mentions in a
comment:
increasing the size of the class, needlessly
2: Copy Semantics
It breaks the compiler generated copy assignment operator. For example, the following code behavior is generally desirable but doesn't work.
A obj1;
// ...
A obj2;
// make changes to 'obj2'
// Update 'obj1' with the changes from 'obj2'
obj1 = obj2; // This copy doesn't work!
More information:
Should I prefer pointers or references in member data?
Assignment operator with reference class member
Thinking in C++, 2nd ed. Volume 1 ©2000 by Bruce Eckel, 11: References & the Copy-Constructor
There are certain rules when using references:
A reference must be initialized when it is created. (Pointers can be initialized at any time.)
Once a reference is initialized to an object, it cannot be changed to refer to another object. (Pointers can be pointed to another object at any time.)
You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage.
It may be possible to implement a custom assignment operator but that's more code to maintain (i.e., another drawback in my opinion).
#include <iostream>
class A
{
private:
int a;
public:
explicit A(int value) : a(value) {}
A& operator=(const A& other)
{
a = other.a;
return *this;
}
const int& ref = a;
};
int main()
{
A obj1(10);
std::cout << "1: " << obj1.ref << "\n";
A obj2(20);
std::cout << "2: " << obj2.ref << "\n";
obj1 = obj2;
std::cout << "1: " << obj1.ref << "\n";
return 0;
}
The idiomatic way to address this issue is to use a proper accessor function.
class A {
private:
int a;
public:
int getA() const { return a; }
};
The standard way to do this in C++ is by making the actual member private but including a public 'getter' method for the interface, as below:
class A{
private:
int a;
public:
int get_a() const { return a; }
A() : a(20) {}
};
int main() {
A obj;
int n = obj.get_a(); // n = 20
return 0;
}
The user cannot set the value of A::a but can use A::get_a to retrieve its value.

Assigning structs including pointers

Consider the following simple program. It just defines two variables A and B from MyStruct and then initializes A.
How can I copy A to B with new pointers for B?
If I use assignment operator, firstMember of B will be assigned with firstMember of A and whenever I change the value of B.firstMember[i], the value of A.firstMember[i] will change. I already know that it can be done with a function, but is there any simpler way? Assuming that MyStruct can have lots pointers, writing a function doesn't seem to be good.
typedef struct MyStruct
{
int * firstMember;
int secondMember;
} MyStruct;
int main()
{
MyStruct A;
MyStruct B;
Initialize(A); // initializes A such that A.firstMember[0] != 5
B = A;
B.firstMember[0] = 5;
cout<<A.firstMember[0]; // prints 5
return 0;
}
This falls into the "Rule of 3". Once you start managing raw pointers inside a class, you probably want to define a custom contructor, destructor, and assignment operator for this very reason.
First of all, I don't understand why among two members, one is a pointer, and other is not. I am presuming that the first member which is a pointer points to a single integer. Here's an improved code snippet for the structure:
struct MyStruct
{
int * firstMember;
int secondMember;
MyStruct& operator = (const MyStruct& src) {
*firstMember = *src.firstMember;
return *this;
}
MyStruct(int a = 0) {
firstMember = new int(a);
secondMember = a;
}
MyStruct(const MyStruct& src) {
// deep copy
firstMember = new int(*src.firstMember);
secondMember = src.secondMember;
}
~MyStruct() {
if (firstMember)
delete firstMember;
}
};
Note, struct is a class with all members declared as public by default. Rather than having Initialize in C-way, better to use C++ proper construct like constructor, and you need to have copy constructor and assignment operator too.

Assignment of base class object to derived class object

Why do I get error: no match for ‘operator=’ in ‘y = x’ in the following code?
Cant the a-component of b not just be assigned to like it was a-object = a-object?
struct a {int i;};
struct b : public a {int j;};
int main()
{
a x;
b y;
x.i = 9;
y.j = 5;
y = x; // error: no match for ‘operator=’ in ‘y = x’
// expected: y.i = 9
return 0;
}
You are not explicitly defining any assignment operators, so the compiler will generate its own default operators for each struct. The compiler's default assignment operator in b takes a b as input and will assign both members. And assignment operators are not automatically inherited when using inheritance. That is why you cannot pass an a to a b - there is no assignment operator in b that takes an a as input. If you want to allow that, you need to tell the compiler as much, eg:
struct a
{
int i;
a& operator=(const a &rhs)
{
i = rhs.i;
return *this;
}
};
struct b : public a
{
int j;
using a::operator=;
b& operator=(const b &rhs)
{
*this = static_cast<const a&>(rhs);
j = rhs.j;
return *this;
}
};
int main()
{
a x;
b y;
b z;
...
y = x; // OK now
y = z; // OK as well
...
return 0;
}
No, because even if the classes are related they are different types.
Think about this, even if it was allowed and it would work like you expected, what would the value of y.j be after the assignment?
As the error states, you need to implement an assignment operator. That is, a function that tells your program how to assign one object to another one. You can find plenty of information on it if you search the web, e.g. on http://www.cplusplus.com/articles/y8hv0pDG/

Temporaries created during assignment operation

if every assignment creates a temporary to copy the object into lvalue, how can you check to see in VC++ 8.0?
class E
{
int i, j;
public:
E():i(0), j(0){}
E(int I, int J):i(I), j(J){}
};
int main()
{
E a;
E b(1, 2);
a = b //would there be created two copies of b?
}
Edit:
Case 2:
class A
{
int i, j;
public:
A():i(0), j(0){}
};
class E
{
int i, j;
A a;
public:
E():i(0), j(0){}
E(int I, int J):i(I), j(J){}
E(const E &temp)
{
a = temp; //temporary copy made?
}
};
int main()
{
E a;
E b(1, 2);
a = b //would there be created two copies of b?
}
From your comment, you made it clear that you didn't quite understand this C++-FAQ item.
First of all, there are no temporaries in the code you presented. The compiler declared A::operator= is called, and you simply end up with 1 in a.i and 2 in a.j.
Now, regarding the link you provided, it has to do with constructors only. In the following code :
class A
{
public:
A()
{
s = "foo";
}
private:
std::string s;
};
The data member s is constructed using std::string parameterless constructor, then is assigned the value "foo" in A constructor body. It's preferable (and as a matter of fact necessary in some cases) to initialize data members in an initialization list, just like you did with i and j :
A() : s("foo")
{
}
Here, the s data member is initialized in one step : by calling the appropriate constructor.
There are a few standard methods that are created automatically for you if you don't provide them. If you write
struct Foo
{
int i, j;
Foo(int i, int j) : i(i), j(j) {}
};
the compiler completes that to
struct Foo
{
int i, j;
Foo(int i, int j) : i(i), j(j)
{
}
Foo(const Foo& other) : i(other.i), j(other.j)
{
}
Foo& operator=(const Foo& other)
{
i = other.i; j = other.j;
return *this;
}
};
In other words you will normally get a copy constructor and an assignment operator that work on an member-by-member basis. In the specific the assignment operator doesn't build any temporary object.
It's very important to understand how those implicitly defined method works because most of the time they're exact the right thing you need, but sometimes they're completely wrong (for example if your members are pointers often a member-by-member copy or assignment is not the correct way to handle the operation).
This would create a temporary:
E makeE( int i, int j )
{
return E(i, j);
}
int main()
{
E a;
a = makeE( 1, 2 );
E b = makeE( 3, 4 );
}
makeE returns a temporary. a is assigned to the temporary and the assignment operator is always called here. b is not "assigned to", as it is being initialised here, and requires an accessible copy-constructor for that to work although it is not guaranteed that the copy-constructor will actually be called as the compiler might optimise it away.