On 11鏈9鏃, 涓嬪崍1鏃45鍒, LR <lr....@superlink.net> wrote:
> Jan Lellmann wrote:
> > 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?
>
> If I've read the standard correctly, I think that section 12.2 says that
> a copy ctor doesn't have to be called here, it can be elided by the
> compiler. But if the copy ctor isn't called the compiler still has to
> apply the rules as if it were called.
>
> LR- 闅愯棌琚紩鐢ㄦ枃* -
>
> - 鏄剧ず寮曠敤鐨勬枃* -
yep,