J
Jan Lellmann
Hello,
could anyone explain to me why the following code fails to compile?
----
#include <iostream>
using namespace std
class A {
private:
friend A createA(); // private default constructor
A() { cout << "default constructor" << endl; };
A(const A& from) { // private copy constructor
cout << "copy constructor" << endl;
}
public:
int get() {
return 1;
}
};
A createA() {
return A();
}
int main(int, char**)
{
createA().get(); // this compiles
const A& ref(createA()); // "error: A::A(const A&) is private"
}
----
The error is the same with g++ and VC++. When the copy constructor is
declared public instead, the code compiles but the copy constructor never
gets called -- why?
Regards,
Jan
could anyone explain to me why the following code fails to compile?
----
#include <iostream>
using namespace std
class A {
private:
friend A createA(); // private default constructor
A() { cout << "default constructor" << endl; };
A(const A& from) { // private copy constructor
cout << "copy constructor" << endl;
}
public:
int get() {
return 1;
}
};
A createA() {
return A();
}
int main(int, char**)
{
createA().get(); // this compiles
const A& ref(createA()); // "error: A::A(const A&) is private"
}
----
The error is the same with g++ and VC++. When the copy constructor is
declared public instead, the code compiles but the copy constructor never
gets called -- why?
Regards,
Jan