3
編譯器如何知道如何正確處理此代碼?C++中的靜態變量構造
struct Foo
{
int bar;
Foo()
{
bar = 3;
}
Foo& operator=(const Foo& other)
{
bar = other.bar;
return *this;
}
int SetBar(int newBar)
{
return bar = newBar;
}
};
static Foo baz;
static Foo someOtherBaz = baz;
static int placeholder = baz.SetBar(4);
someOtherBaz.bar
的終值是多少?
@Samuel:我有一個疑問。在構建'someOtherBaz'後,使用編譯器提供的拷貝構造函數,賦值運算符函數將被調用'static Foo someOtherBaz = baz;'?否則,這裏的任務是什麼意思? – legends2k 2010-02-14 07:13:06
@ legends2k - 對於單參數構造函數,'TYPE t = arg'與'TYPE t(arg)'相同。 – 2010-02-14 08:25:14
@ legend22K - 代碼中沒有賦值 - 使用=符號不是賦值,它是複製構造函數的調用。 – 2010-02-14 09:46:07