2010-02-04 74 views
1

如果是,它应该做什么?以下有效的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做)。

谢谢!

回答

8

我不认为它是。 (并且Comeau与我一致。)你不能像这样定义一个构造函数。

在C++中,结构体名称是一等公民。没有必要使用来自C的旧的typedef技巧。另外,dc应该在成员初始化列表中初始化。这将是有效的(和更好的(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(我们没有见过)是一个拥有多个内置成员的类,这可能比每个副本传递它要便宜得多。

+3

或者更好的是'Foo(const Dog&dog,Cat * cat):d(dog),c(cat){}'...避免了实例化'd'的开销,只是用参数覆盖它。通过引用传递更便宜和更多的C++。 – 2010-02-04 06:49:50

+0

@ Travis:当您添加评论时,我正在更改代码。 ':)' – sbi 2010-02-04 06:50:35

+0

@ Travis - 宝贝步骤。 :) – 2010-02-04 06:51:24

0

它看起来像它定义了一个名为Foo_Struct的结构,它包含Dog的一个实例,有一个指向猫的指针,并且有一个构造函数,它接受Dog的实例,指向Cat的指针,并将它们分配给它自己。

然后在堆栈上创建Foo实例。

编辑:我不确定第三行是构造函数还是别的。

3

这几乎是合法的,但有一个错误。结构就像一个类,除了默认保护是公开的而不是私有的。

好吧,让我们来分析一下:

// 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; 
+0

@Samual:我现在将文本改为“...正在创建一个名为...的结构”,以“...正在创建一个名为...的结构_type_”。至少在我住的地方(非母语为英语的人),“创建结构”应该被理解为创建结构类型的_object_,而不是创建结构类型。不过,这可能只是我们这里的非本地人。无论如何,因为你是正确的,否则。 – sbi 2010-02-04 07:00:52

+0

@sbi - 感谢您的反馈意见;我将它改为“正在定义一个名为”的结构。 – 2010-02-04 07:12:52

5

不,它不是。在构造函数中不能有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的答案。

4

大多数情况下,虽然structtypedef是不必要的。更好用C++编写为:

class Foo { 
    public: 
    Foo(Dog dog, Cat *cat) : d(dog), c(cat) {} 
    private: 
    Dog d; 
    Cat *c; 
}; 

行由行:

class Foo { 

同为struct Foo。在classstruct之间的C++唯一区别在于struct的成员默认是公共的,而class的成员是私有的。但是,我们需要一些公共成员的地方,所以我们避开与...

public: 

一切后,这是公开的,可以通过与Foo对象任何人都可以访问。

Foo(Dog dog, Cat *cat) : d(dog), c(cat) {} 

这是Foo的构造函数。它创建了一个新的Foo对象,给定DogCat *: d(dog), c(cat)是一个初始化程序列表。它与this->d = dog; this->c = cat;相同,除了可能更快。如果你不想这样做,你可以离开this->,除非在某处出现命名冲突。 {}是函数体,因为我们将赋值移到了初始值设定项列表中。

private: 

public:相反。在此之后宣布的事情只能在我们班级内部进行访问,并且仅供内部使用。

Dog d; 
    Cat *c; 

这些都是类的内部变量,就像一个struct的成员。

+0

'class'与'struct'不完全相同,因为'class'默认为私有可见性。 – kennytm 2010-02-04 07:05:32