Help? I really have no idea what's happening here?
Why in line 3 of assignments it calls operator= of A, when its an assignment of B to B?
class A{
public:
A& operator=(const A&){cout << "A assignment" << endl;return *this;}
};
class B:public A{
public:
A& operator=(const A&){cout << "B assignment" << endl;return *this;}
};
int main() {
A a;
B b;
B b2;
a=b; // output: A assignment
b=a; // output: B assignment
b=b2; // output: A assignment WHY??
return 0;
}
There's still a compiler-generated assignment operator in class B (it's overloaded); unlike how this works with constructors, defining one or more assignment operator overloads does not prevent the compiler from generating a copy assignment operator when one is lacking. The compiler generated one calls A::operator=. And it's the better match for an argument of type B.
You've defined an assignment operator in B, but there's also another implicit copy-assignment operator generated by the compiler:
B& B::operator=(B const&);
This is a better match than the one that takes A const& so it is chosen in the assignment b = b2 (since b2 is a B it doesn't require a derived-to-base conversion for the one that takes an A). The implicit copy-assignment operator calls the copy-assignment operator of the base class which you wrote:
B& B::operator=(B const& b) {
A::operator=(b);
return *this;
}
This is why it looks like A::operator=(A const&) is being chosen by the assignment.
Related
i have a question about assigning one member of the derived class to another of the same derived class,
Can someone please help me understand why this code is printing 14 and not 34?
i dont understand when b2.nb is getting the value of b1.nb, because in this code:
A& operator=(const A& a){
na = a.na*2;
return *this;
}
there is no assignment of b1.nb to b2.nb
class A{
public:
A(int i){na = i;}
A& operator=(const A& a){
na = a.na*2;
return *this;
}
int na;
};
class B : public A{
public:
B(int i, int j): A(j){nb = i;}
int nb;
};
int main()
{
B b1(1,2);
B b2(3,4);
b2 = b1;
cout<<b2.nb<<b2.na;
}
From cppreference
Implicitly-declared copy assignment operator
If no user-defined copy assignment operators are provided for a class type (struct, class, or union), the compiler will always declare one as an inline public member of the class.
Your B class never declares a copy assignment operator (i.e. an operator with a signature compatible with B& operator=(const B&)). The fact that A does is irrelevant here. B does not declare one, so C++ generates one. That generated copy assignment operator calls the parent class' assignment operator and then assigns any variables declared in B directly, in your case nb.
I know that the assignment operator is not inherited by derived classes, instead the compiler will create a default one if it is not redeclared. But I do not understand why the output of the following code snippet is Base operator=:
#include <iostream>
using namespace std;
class B {
protected:
int h;
public:
B& operator=(const B& ob){
if (this!=&ob) {
h = ob.h;
cout << "Base operator=\n";
}
return *this;
}
};
class D: public B {
protected:
float r;
public:
};
int main() {
D a, b;
a = b;
return 0;
}
Doesn't that mean that when calling a = b the base B& operator=(const B& ob, so isn't it inherited? Where am I wrong ?
The generated assignment is "all the base assignments, in order of inheritance declaration", so your generated assignment is essentially
D& operator=(const D& d)
{
B::operator=(d);
return *this;
}
If you were to derive from both B and C - in that order; class D: B, C - it would be equivalent to
D& operator=(const D& d)
{
B::operator=(d);
C::operator=(d);
return *this;
}
That is, the assignment is not inherited, but it's used.
With the expression a = b, the compiler generated assignment operator for D calls the user-defined assignment operator in B.
Yes you are correct that assignment operators are not inherited.
I have a piece of code that looks something like this:
class B
{
private:
const A& obj;
size_t val;
// private - prohibited
B& operator=(const B& other);
public:
B(const A& obj_): obj(obj_)
{
val = 0;
}
};
class C
{
void func()
{
A a1;
B b1(a1);
B b2 = b1; // should throw error?
}
}
Class B has a private assignment operator. Nonetheless assignment in C::func() compiles without errors. But how?
B b2 = b1 does not use the assignment operator, because it's not assignment, it's copy initialization - using constructor, not assignment operator.
See also copy initialization
That's the copy constructor getting called, not the assignment operator.
The reason being is that when you initialize a variable, a constructor (in this case, the copy constructor) gets called.
B b2 = b1; // construction
This is different from assignment
b2 = b1; // b2 is already declared, so assignment
To stop this behavior, just delete your copy constructor.
There is the following definition of the copy constructor:
A non-template constructor for class X is a copy constructor if its
first parameter is of type X&, const X&, volatile X& or const volatile X&,
and either there are no other parameters or else all
other parameters have default arguments (8.3.6).
Note, that the definiton isn't concerned with the convertions, but the following program works fine:
#include <iostream>
struct B{ };
struct A : B
{
A(){ }
A(const B&){ }
};
B b;
A a = b;
int main(){ }
DEMO
and it produces the output
B()
B()
A(const B&)
It's not clear to me, I expected the program doesn't print A(const B&), because by the definition A(const B&) is not a copy constructor, therefore one is implictly defined as A::A(const A&) with default initializtion which doesn't produce any side-effect.
Couldn't you clarify what's wrong with that reason?
This is known as a converting constructor (ยง12.3.1). b is copy-initialized into a and A::A(const B&) is selected to perform the conversion. The result of the conversion is used to direct-initialize a.
A a = b;
This would call the conversion constructor of class A.
A(const B&){ } << This is conversion constructor for class A which defines conversion from B to A
Its same like we declare
A(int i) {} << Convert int to class A object.
Even if you remove the relationship between class A and B then also it lead to that constructor call.
This concerns the resolution of C++ Issue http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1402 . Summary:
template<typename T>
struct wrap
{
wrap() = default;
wrap(wrap&&) = default;
wrap(const wrap&) = default;
T t;
};
struct S {
S(){}
S(const S&){}
S(S&&){}
};
typedef wrap<const S> W;
// Error, defaulted move constructor of "wrap<const S>" is deleted!
W get() { return W(); }
(The issue is that we are getting an error for this snippet, even though the compiler could simply use the copy constructor of "S", as it does when the user explicitly writes the move constructor of "wrap". The issue was resolved to ignore deleted move constructors (and assignment operators) during overload resolutions, hence using the copy constructor above as desired.)
When the resolution was drafted, the following comment was made about this resolution:
There are no additional restrictions for implicit/defaulted move operations relative to copy. This means that move assignment in a virtual base is dangerous (and compilers should probably warn) [...] But we decided in Batavia that we weren't going to preserve all C++03 code against implicit move operations, and I think this resolution provides significant performance benefits.
Can someone please describe what the concern with virtual base move assignment operators is?
Consider:
#include <iostream>
struct A
{
A() = default;
A(const A&) = default;
A(A&&) = default;
A& operator=(const A&) {std::cout << "operator=(const A&)\n"; return *this;}
A& operator=(A&&) {std::cout << "operator=(A&&)\n"; return *this;}
};
struct B
: virtual public A
{
};
struct C
: virtual public A
{
};
struct D
: public B,
public C
{
};
int
main()
{
D d1, d2;
d1 = std::move(d2);
}
I believe this program should output:
operator=(A&&)
operator=(A&&)
For me it actually outputs:
operator=(const A&)
operator=(const A&)
But I think this is just a clang bug (compiled with -std=c++1y). If I am correct about what the output should be, then the danger is that the move assignment operator is called twice. This is harmless (though potentially expensive) with the copy assignment operator, but not with the move assignment operator.