【发布时间】:2014-03-25 09:39:26
【问题描述】:
有没有一种方法可以创建驻留在另一个类中的类的实例? 例如:
class foo
{
public:
foo()
{
//Constructor stuff here.
}
class bar
{
bar()
{
//Constructor stuff here.
}
void action(foo* a)
{
//Code that does stuff with a.
}
}
void action(bar* b)
{
//Code that does stuff with b.
}
}
现在我只想在我的 main() 中创建一个 bar 的实例,如下所示:
foo* fire;
bar* tinder;
但是 bar 没有在这个范围内声明。我在一个类中使用一个类的原因是因为它们都使用将另一个类作为参数的方法,但我需要 main() 中每个类的实例。我能做什么?
【问题讨论】:
-
在类定义后缺少分号。
标签: c++