sth*_*sth 443
在C++中,a class
和a 之间的唯一区别struct
是成员和基类在类中默认是私有的,而在结构中它们默认是公共的.
所以结构体可以有构造函数,语法与类相同.
nos*_*nos 156
struct TestStruct {
int id;
TestStruct() : id(42)
{
}
};
Run Code Online (Sandbox Code Playgroud)
小智 39
所有上述答案在技术上都回答了提问者的问题,但我想我会指出一个可能遇到问题的案例.
如果你声明你的结构如下:
typedef struct{
int x;
foo(){};
} foo;
Run Code Online (Sandbox Code Playgroud)
尝试声明构造函数时会遇到问题.这当然是因为您实际上没有声明一个名为"foo"的结构,您已经创建了一个匿名结构并为其指定了别名"foo".这也意味着您将无法在cpp文件中将"foo"与作用域运算符一起使用:
foo.h中:
typedef struct{
int x;
void myFunc(int y);
} foo;
Run Code Online (Sandbox Code Playgroud)
Foo.cpp中:
//<-- This will not work because the struct "foo" was never declared.
void foo::myFunc(int y)
{
//do something...
}
Run Code Online (Sandbox Code Playgroud)
要解决此问题,您必须执行以下操作:
struct foo{
int x;
foo(){};
};
Run Code Online (Sandbox Code Playgroud)
或这个:
typedef struct foo{
int x;
foo(){};
} foo;
Run Code Online (Sandbox Code Playgroud)
后者创建一个名为"foo"的结构并为其赋予别名"foo",因此struct
在引用它时不必使用该关键字.
Cha*_*hap 35
是的,但如果你的结构是联盟,那么你就不能.它与班级相同.
struct Example
{
unsigned int mTest;
Example()
{
}
};
Run Code Online (Sandbox Code Playgroud)
工会不允许结构中的构造函数.您可以在联合上创建构造函数.这个问题涉及工会中的非平凡建构者.
Luq*_*aan 30
正如其他答案所提到的,结构基本上被视为C++中的一个类.这允许您拥有一个构造函数,该构造函数可用于使用默认值初始化结构.下面,构造函数接受sz
并b
作为参数,并将其他变量初始化为某些默认值.
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) {}
};
Run Code Online (Sandbox Code Playgroud)
用法:
unsigned char *bptr = new unsigned char[1024];
blocknode *fblock = new blocknode(1024, btpr);
Run Code Online (Sandbox Code Playgroud)
GMa*_*ckG 16
是.结构就像一个类,但public:
在类定义和继承时默认为:
struct Foo
{
int bar;
Foo(void) :
bar(0)
{
}
}
Run Code Online (Sandbox Code Playgroud)
考虑到您的其他问题,我建议您阅读一些教程.他们会比我们更快,更完整地回答您的问题.
SwD*_*n81 13
struct HaveSome
{
int fun;
HaveSome()
{
fun = 69;
}
};
Run Code Online (Sandbox Code Playgroud)
我宁愿在构造函数内初始化,所以我不需要保持顺序.
hea*_*vyd 12
是的,C++中的结构和类是相同的,除了结构成员默认是公共的,而类成员默认是私有的.你可以在一个结构中做的任何事情.
struct Foo
{
Foo()
{
// Initialize Foo
}
};
Run Code Online (Sandbox Code Playgroud)
Ste*_*e L 12
请注意,有一个有趣的区别(至少使用MS C++编译器):
如果你有一个像这样的普通香草结构
struct MyStruct {
int id;
double x;
double y;
} MYSTRUCT;
Run Code Online (Sandbox Code Playgroud)
然后你可能会在其他地方初始化这样的对象数组,如下所示:
MYSTRUCT _pointList[] = {
{ 1, 1.0, 1.0 },
{ 2, 1.0, 2.0 },
{ 3, 2.0, 1.0 }
};
Run Code Online (Sandbox Code Playgroud)
但是,只要向MyStruct添加用户定义的构造函数(如上面讨论的那些),就会出现如下错误:
Run Code Online (Sandbox Code Playgroud)'MyStruct' : Types with user defined constructors are not aggregate <file and line> : error C2552: '_pointList' : non-aggregates cannot be initialized with initializer list.
所以这至少是结构和类之间的另一个区别.这种初始化可能不是很好的OO实践,但它在我支持的传统WinSDK c ++代码中出现.只是你知道......
Kul*_*ani 10
在c ++中,struct和c ++类只有一个区别,默认情况下,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;
}
Run Code Online (Sandbox Code Playgroud)
小智 7
是的,这里的构造函数可能是一个例子:
#include<iostream.h>
struct a {
int x;
a(){x=100;}
};
int main() {
struct a a1;
getch();
}
Run Code Online (Sandbox Code Playgroud)
在C++中struct
,class
除了struct's
默认成员访问说明符是public
&class 之外,它们都是相同的private
.
使用struct
C++ 的原因是C++是C的超集,必须向后兼容legacy C types
.
例如,如果语言用户试图legacy-c.h
在他的C++代码中包含一些C头文件并且它包含struct Test {int x,y};
.成员struct Test
应该像C一样可以访问.
还有一个示例,但在构造函数中设置值时使用此关键字:
#include <iostream>
using namespace std;
struct Node {
int value;
Node(int value) {
this->value = value;
}
void print()
{
cout << this->value << endl;
}
};
int main() {
Node n = Node(10);
n.print();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
符合GCC 8.1.0。
语法与C ++中的类相同。如果您知道在c ++中创建构造函数,则在struct中也是如此。
struct Date
{
int day;
Date(int d)
{
day = d;
}
void printDay()
{
cout << "day " << day << endl;
}
};
Run Code Online (Sandbox Code Playgroud)
Struct在c ++中可以将所有内容都作为类。如前所述,区别仅在于默认情况下C ++成员具有私有访问权限,但在struct中它是公共访问权限。但是根据编程考虑,请将struct关键字用于仅数据结构。将class关键字用于具有数据和功能的对象。
归档时间: |
|
查看次数: |
522432 次 |
最近记录: |