0 c++ constructor class initializing
我正在尝试在另一个构造函数中初始化类构造函数.GCC引发了错误,'Type'foo'没有调用操作符.这个伪代码应该解释我的意图.
class foo {
type arg1, arg2;
foo (type _arg1, type _arg2) {
_arg1=arg1;
_arg2=arg2;
}
}
class foo2 {
foo member;
foo2(type _arg1, type _arg2) {
member(_arg1, _arg2);
}
}
Run Code Online (Sandbox Code Playgroud)
两个问题:
首先,你的foo构造函数应该是公开的,正如Mark的回答所述.
其次,要使用其构造函数初始化成员,您应该使用以下语法:
foo2(type _arg1, type _arg2) :
member(_arg1, _arg2)
{ /* code */ }
Run Code Online (Sandbox Code Playgroud)