S
SzH
The code below demonstrates that the copy constructor of moo is not
called on the first line of main. In spite of this, g++ (version 4.1.2)
refuses to compile it if I make the copy constructor private. But the
Digital Mars compiler does not complain.
Which compiler is right? (And if gcc is right, why does this restriction
exist?)
Szabolcs
#include <iostream>
struct moo {
int data;
moo(int n) : data(n) {
std::cout << "constructor\n";
}
// private:
moo(const moo &m) : data(m.data) {
std::cout << "copy constructor\n";
}
};
moo fun() {
return moo(3);
}
int main() {
moo m = fun();
return 0;
}
called on the first line of main. In spite of this, g++ (version 4.1.2)
refuses to compile it if I make the copy constructor private. But the
Digital Mars compiler does not complain.
Which compiler is right? (And if gcc is right, why does this restriction
exist?)
Szabolcs
#include <iostream>
struct moo {
int data;
moo(int n) : data(n) {
std::cout << "constructor\n";
}
// private:
moo(const moo &m) : data(m.data) {
std::cout << "copy constructor\n";
}
};
moo fun() {
return moo(3);
}
int main() {
moo m = fun();
return 0;
}