help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

base constructor


From: solid
Subject: base constructor
Date: 8 Mar 2006 16:08:19 -0800
User-agent: G2/0.2

Hi, I'm using g++ version 3.4.2  and the following program behaves
strangly:
class A {
public:
A(bool b) : b_(b) {}
A(const A& a) { b_ = !a.b_; }
bool get() const { return b_; }
private:
bool b_;
};

#include <iostream>
using namespace std;
int main() {
 A x = A(A(true));
 cout << x.get() << endl;
}

It outputs 1 rather than 0, specifically the copy constructor is not
called. Changing
 A x = A(A(true));
to
 A y = A(true);
 A z = A(y);
 cout << z.get() << endl;
outputs 0.
WHY???



reply via email to

[Prev in Thread] Current Thread [Next in Thread]