在C + +的结构构造?

一个struct可以在C ++中有一个构造函数吗?

我一直在试图解决这个问题,但我没有得到的语法。

在C ++中, classstruct之间唯一的区别在于成员和基类在类中是默认私有的,而在结构中它们是默认公用的。

所以结构体可以有构造函数,语法和类相同。

 struct TestStruct { int id; TestStruct() : id(42) { } }; 

是的,但是如果你有一个工会的结构,那么你不能。 这和一个class级是一样的。

 struct Example { unsigned int mTest; Example() { } }; 

工会不允许在结构中的构造函数。 你可以在联合上做一个构造函数。 这个问题涉及工会中不平凡的build造者。

以上所有的答案在技术上回答了提问者的问题,但只是想我会指出你可能遇到问题的情况。

如果你像这样声明你的结构:

 typedef struct{ int x; foo(){}; } foo; 

尝试声明构造函数时会遇到问题。 这当然是因为你实际上并没有声明一个名为“foo”的结构,你已经创build了一个匿名结构并为其分配了别名“foo”。 这也意味着你将无法在cpp文件中使用范围操作符“foo”:

foo.h中:

 typedef struct{ int x; void myFunc(int y); } foo; 

Foo.cpp中:

 //<-- This will not work because the struct "foo" was never declared. void foo::myFunc(int y) { //do something... } 

要解决这个问题,你必须这样做:

 struct foo{ int x; foo(){}; }; 

或这个:

 typedef struct foo{ int x; foo(){}; } foo; 

如果后者创build一个名为“foo”的结构并为其提供别名“foo”,所以在引用它时不必使用struct关键字。

正如其他答案所提到的,一个结构基本上被当作C ++中的一个类来处理。 这允许你有一个构造函数可以用来初始化结构的默认值。 下面,构造函数将szb作为参数,并将其他variables初始化为一些默认值。

 struct blocknode { unsigned int bsize; bool free; unsigned char *bptr; blocknode *next; blocknode *prev; blocknode(unsigned int sz, unsigned char *b, bool f = true, blocknode *p = 0, blocknode *n = 0) : bsize(sz), free(f), bptr(b), prev(p), next(n) {} }; 

用法:

 unsigned char *bptr = new unsigned char[1024]; blocknode *fblock = new blocknode(1024, btpr); 

是。 结构就像一个类,但默认为public: :,在类的定义和inheritance时:

 struct Foo { int bar; Foo(void) : bar(0) { } } 

考虑到你的其他问题,我build议你通读一些教程 。 他们会比我们更快,更完整地回答你的问题。

 struct HaveSome { int fun; HaveSome() { fun = 69; } }; 

我宁愿在构造函数内初始化,所以我不需要保持顺序。

是的C ++中的结构和类是相同的,除了结构成员默认是公共的,而类成员默认是私有的。 你可以在课堂上做任何事情,你应该可以在一个结构中做。

 struct Foo { Foo() { // Initialize Foo } }; 

请注意,有一个有趣的差异(至less在MS C ++编译器中):


如果你有这样一个普通的香草结构

 struct MyStruct { int id; double x; double y; } MYSTRUCT; 

那么你可能会在其他地方初始化这样的对象数组:

 MYSTRUCT _pointList[] = { { 1, 1.0, 1.0 }, { 2, 1.0, 2.0 }, { 3, 2.0, 1.0 } }; 

但是,只要将用户定义的构造函数添加到MyStruct(比如上面讨论的构造函数),就会发生如下错误:

  'MyStruct' : Types with user defined constructors are not aggregate <file and line> : error C2552: '_pointList' : non-aggregates cannot be initialized with initializer list. 

所以这是结构和类之间的至less一个其他区别。 这种初始化可能不是很好的OO练习,但它在我所支持的传统WinSDK c ++代码中出现在各处。 就这样你知道…

c ++中,structc ++类只有一个区别,默认情况下,struct成员是public的,而class成员是private的。

 /*Here, C++ program constructor in struct*/ #include <iostream> using namespace std; struct hello { public: //by default also it is public hello(); ~hello(); }; hello::hello() { cout<<"calling constructor...!"<<endl; } hello::~hello() { cout<<"calling destructor...!"<<endl; } int main() { hello obj; //creating a hello obj, calling hello constructor and destructor return 0; } 

是的,有可能在这里有构造函数是一个例子:

 #include<iostream.h> struct a { int x; a(){x=100;} }; int main() { struct a a1; getch(); } 

在C ++中,除了struct's默认成员访问说明符public &class具有private外, structclass是相等struct's

在C ++中有struct的原因是C ++是C的超集,并且必须向后兼容legacy C types

例如,如果语言用户试图在他的C ++代码中包含一些C头文件legacy-ch ,并且它包含struct Test {int x,y};struct Test成员应该像C一样可访问。

在C ++中,我们可以像类一样声明/定义结构,并具有结构的构造函数/析构函数,并定义其中的variables/函数。 唯一的区别是定义的variables/函数的默认范围。 除了上述差异之外,大多数情况下你应该能够使用结构模仿类的function。

语法与C ++中的类相同。 如果你知道在c ++中创build构造函数,那么它在结构中是相同的。

 struct Date { int day; Date(int d) { day = d; } void printDay() { cout << "day " << day << endl; } }; 

Struct可以把所有的东西都当作c ++中的类。 正如前面所说的差异只是默认情况下C ++成员有私人访问,但在结构它是公开的。但根据编程的考虑使用结构关键字的数据专用结构。 对具有数据和函数的对象使用class关键字。