可能重复:
C++ - 我什么时候应该在类中使用指针成员
我已经使用 c++ 多年,但我仍然有一些问题没有回答。我正在定义一个 c++ 类,但我对此有一些疑问
问题1:我是否应该使用指针存储投资?
Investment* investment;
// or
Investment investment;
问题2:为什么?
问题3:如果我使用Investment investment;
我应该以这种方式返回参考Investment & getInvestment();
吗?
我知道我的代码的两种方式都可以工作,但我想知道执行此操作的有效方法..
代码如下:
class Urgency
{
private:
Investment* investment;
public:
Urgency(Investment* investment);
~Urgency(void);
Investment* getInvestment();
};