我是C++新手,我的问题可能很简单,但我无法找到解决方案。初始化类内的类
我有两个班,S
和L
。 S
看起来是这样的:
class S
{
private:
int m_x;
public:
S(int x) : m_x(x)
{
}
int m_y = 2*m_x; // some random action in S
};
现在我有一个第二类L
,在这里我想初始化S
-object:
class L
{
private:
S s(10); // 10 is just some random value
int m_y;
public:
L(int y): m_y(y)
{
}
// ignore the rest for now.
};
这在s(10)
初始化的行会产生错误error: expected identifier before numeric constant
。
我不明白为什么我不能这样做。我怎么能解决这个问题?如果我想初始化对象S s(m_y)
而不是?
移动s'的'初始化成'L'的构造。 – DimChtz
尝试在类“L”的构造函数中初始化's'。 – wdc