<分区>
对于类,你可以说:
class Test{
int a;
Test(int a);
}
Test::Test(int a) {
this->a=a;
}
当在类外声明时,函数名前面有“classname::”。
我将如何为结构执行此操作?
struct Test {
int a;
Test(int a);
}
我如何在结构声明之外为这个结构测试编写函数,以便它只能由测试结构调用?
<分区>
对于类,你可以说:
class Test{
int a;
Test(int a);
}
Test::Test(int a) {
this->a=a;
}
当在类外声明时,函数名前面有“classname::”。
我将如何为结构执行此操作?
struct Test {
int a;
Test(int a);
}
我如何在结构声明之外为这个结构测试编写函数,以便它只能由测试结构调用?
最佳答案
同理。 C++ 中 struct
和 class
的区别只是成员的默认可见性(class
私有(private),struct
公有) .
其实,它不仅仅是函数,它是class
/struct
Test
的构造函数。
关于C++:为该结构之外的结构编写函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27922462/