English 中文(简体)
C+++ 复制构造器
原标题:C++ Copy Constructor

我有一个关于初始化的语法问题

引自http://en.wikipedia.org/wiki/Copy_constructor

X a = X();
// valid given X(const X& copy_from_me) but not valid given X(X& copy_from_me)               
// because the second wants a non-const X&               
// to create a, the compiler first creates a temporary by invoking the default constructor               
// of X, then uses the copy constructor to initialize a as a copy of that temporary.                
// However, for some compilers both the first and the second actually work.

#include <iostream>

class Foo
{
public:
    Foo()
    {
        std::cout << "Default Constructor called" << std::endl;
    }

    Foo(const Foo& other)
    {
        std::cout << "Copy constructor called" << std::endl;
    }

    Foo& operator=(const Foo& rhs)
    {
        std::cout << "Assignment operator called" << std::endl;
    }
};

int main()
{
    Foo b = Foo(); //case 1:default 
    Foo c = Foo(a); //case 2: copy constructor
}

Case 1:
Upon changing the parameter from const to non const in the copy constructor, case 1 won t compile as expected from wikipedia. However, when ran using the proper copy constructor, it only calls the default constructor. Why doesn t it also call the copy constructor? Is this an optimization done at compile-time?

Case 2:
The answer to case 1 will probably answer case 2 for me, but why does this only call the copy constructor once?

问题回答
Foo b = Foo();

此窗体需要有一个有效的匹配复制件构建器才能存在, 但复制件可以优化使用 。 它可能被优化使用这一事实并不能放松对构建器存在的要求 。

由于 Foo () 产生临时引用, 临时无法与非默认引用捆绑起来。 当您将参数 Const 引用( 或将您的 c- tor 擦掉并使用编译器生成的 c- tor ) 进行参数引用时, 它就不再匹配了, 因为临时引用可以与 const 引用捆绑在一起 。

X () is a临时, 所以您无法将其绑定为非 accent 引用( 虽然 MSVS 有允许的扩展名) 。

(1) 是, 是编译器优化

2) 非法,因为 a 并不存在。但原则上,再次,是的,是一个编译优化。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签