3个回答
2013-11-28
展开全部
只需要在A的构造函数中写
A():_B(_school)
在A后面private加上
B _B
就可以嵌入了
追问
没看懂,能仔细说说嘛
追答
//你看看这个就懂了
#include <iostream>
class Matter
{
public:
Matter(int id)
:_identifier(id)
{
std::cout<<" Matter for "<<_identifier<<" created\n";
}
~Matter()
{
std::cout<<" Matter for "<<_identifier<<" annihilated\n";
}
private:
const int _identifier;
};
class World
{
public:
World(int id)
: _identifier(id),
_matter(_identifier)//Initializing the embedded frame
{
std::cout<<" Hello from world "<<_identifier<<".\n";
}
~World()
{
std::cout<<" Goodbye from world "<<_identifier<<".\n";
}
private:
const int _identifier;
const Matter _matter; // Embedded object of Matter type
};
World TheUniverse(1);
int main()
{
World smallWorld(2);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询