如果是,它應該做什麼?以下有效的C++代碼?
typedef struct Foo_struct{
Dog d;
Cat* c;
struct Foo_struct(Dog dog, Cat* cat){ this->d = dog; this->c = cat;}
} Foo;
(背景故事:移植用Visual C++(Windows上的程序),以G ++(MacOSX上);不知道這是什麼代碼suppoesd做)。
謝謝!
如果是,它應該做什麼?以下有效的C++代碼?
typedef struct Foo_struct{
Dog d;
Cat* c;
struct Foo_struct(Dog dog, Cat* cat){ this->d = dog; this->c = cat;}
} Foo;
(背景故事:移植用Visual C++(Windows上的程序),以G ++(MacOSX上);不知道這是什麼代碼suppoesd做)。
謝謝!
我不認爲它是。 (並且Comeau與我一致。)你不能像這樣定義一個構造函數。
在C++中,結構體名稱是一等公民。沒有必要使用來自C的舊的typedef
技巧。另外,d
和c
應該在成員初始化列表中初始化。這將是有效的(和更好的(C++):
struct Foo {
Dog d;
Cat* c;
Foo(Dog dog, Cat* cat) : d(dog), c(cat) {}
};
的代碼定義一個結構(在C++中,同爲一個類,不同之處在於它的成員默認都是公共的)使用構造其成員時初始化創建
編輯:由於特拉維斯在他的評論中說,你可能要考慮通過dog
作爲const
參考而不是複製:
Foo(const Dog& dog, Cat* cat) : d(dog), c(cat) {}
如果Dog
(我們沒有見過)是一個擁有多個內置成員的類,這可能比每個副本傳遞它要便宜得多。
它看起來像它定義了一個名爲Foo_Struct的結構,它包含Dog的一個實例,有一個指向貓的指針,並且有一個構造函數,它接受Dog的實例,指向Cat的指針,並將它們分配給它自己。
然後在堆棧上創建Foo實例。
編輯:我不確定第三行是構造函數還是別的。
這幾乎是合法的,但有一個錯誤。結構就像一個類,除了默認保護是公開的而不是私有的。
好吧,讓我們來分析一下:
// The next line is defining a struct called "Foo_struct", it's also
// saying it's going to give an alternate type name (that's the typedef).
// The alternate type name comes after the definition.
typedef struct Foo_struct{
// The structure has a Dog element (this means we need to have seen
// the definition of Dog already).
Dog d;
// And has a pointer to cat (this means we need to have at least seen
// a declaration of Cat)
Cat* c;
// Okay, this is definining a constructor. The constructor must be
// called with a Dog object and a pointer to a cat which the constructor
// will save in the object.
//
// Here is the one error. That 'struct' at the start shouldn't
// be there (commenting out to make the code legal).
/* struct */ Foo_struct(Dog dog, Cat* cat){ this->d = dog; this->c = cat;}
// And here we close out the struct and also finish off the typedef
// started on the first line.
} Foo;
@Samual:我現在將文本改爲「...正在創建一個名爲...的結構」,以「...正在創建一個名爲...的結構_type_」。至少在我住的地方(非母語爲英語的人),「創建結構」應該被理解爲創建結構類型的_object_,而不是創建結構類型。不過,這可能只是我們這裏的非本地人。無論如何,因爲你是正確的,否則。 – sbi 2010-02-04 07:00:52
@sbi - 感謝您的反饋意見;我將它改爲「正在定義一個名爲」的結構。 – 2010-02-04 07:12:52
不,它不是。在構造函數中不能有struct
。一個有效的C++代碼以最小的變化將
typedef struct Foo_struct{
Dog d;
Cat* c;
Foo_struct(Dog dog, Cat* cat){ this->d = dog; this->c = cat;} // <-- remove the "struct"
} Foo;
爲了更好的辦法看到@ SBI的答案。
大多數情況下,雖然struct
和typedef
是不必要的。更好用C++編寫爲:
class Foo {
public:
Foo(Dog dog, Cat *cat) : d(dog), c(cat) {}
private:
Dog d;
Cat *c;
};
行由行:
class Foo {
同爲struct Foo
。在class
和struct
之間的C++唯一區別在於struct
的成員默認是公共的,而class
的成員是私有的。但是,我們需要一些公共成員的地方,所以我們避開與...
public:
一切後,這是公開的,可以通過與Foo
對象任何人都可以訪問。
Foo(Dog dog, Cat *cat) : d(dog), c(cat) {}
這是Foo的構造函數。它創建了一個新的Foo
對象,給定Dog
和Cat *
。 : d(dog), c(cat)
是一個初始化程序列表。它與this->d = dog; this->c = cat;
相同,除了可能更快。如果你不想這樣做,你可以離開this->
,除非在某處出現命名衝突。 {}
是函數體,因爲我們將賦值移到了初始值設定項列表中。
private:
與public:
相反。在此之後宣佈的事情只能在我們班級內部進行訪問,並且僅供內部使用。
Dog d;
Cat *c;
這些都是類的內部變量,就像一個struct
的成員。
'class'與'struct'不完全相同,因爲'class'默認爲私有可見性。 – kennytm 2010-02-04 07:05:32
或者更好的是'Foo(const Dog&dog,Cat * cat):d(dog),c(cat){}'...避免了實例化'd'的開銷,只是用參數覆蓋它。通過引用傳遞更便宜和更多的C++。 – 2010-02-04 06:49:50
@ Travis:當您添加評論時,我正在更改代碼。 ':)' – sbi 2010-02-04 06:50:35
@ Travis - 寶貝步驟。 :) – 2010-02-04 06:51:24