如果我有以下代码(简化版本的问题):C++原型初始化列表?
class TestA
{
public:
int A,B,C;
TestA(){A = 1; B = 5; C = 10;}
};
//This is a referencing class to allow for universal and consistent operations
class TestB
{
public:
int &A, &B, &C; //Note references
TestB(TestA &A) : A(A.A), B(A.B), C(A.C){}; //This is fine
TestB(TestC &C) : A(C.A), B(C.B), C(C.C){}; //This needs to be prototyped
};
//Similar class to TestA but in the main program would have...
//...many different and conflicting variables and has to be treated as stand alone
class TestC
{
public:
int A, B, C;
int Size;
void Function()
{
TestB B(*this); //This uses TestB. TestB cannot be prototyped.
//etc etc
}
};
我想知道,是否有可能原型初始化器基于列表构造?
如果不是,有什么选择?请记住引用必须立即初始化。
怎么办你的意思是“原型”?你是否指的是你需要一个单独的声明和定义,以便'TestC'可用? –
你是什么意思在ctors中的“原型”初始值设定项列表? –
TestC将其自身的副本传递给TestB,以便TestB可以返回到TestC。然后,TestA将自己复制到另一个TestB,允许两个类(TestA和TestC)相互操作,并传递给TestB预期功能,而不需要向下转换或向上转换(这可能涉及切片,因此存在问题)。 – SSight3