如何返回聚合在另一个类中的点对象
How do I return a point object aggregated in a another class?
如何返回聚合在另一个类中的点对象?我试图返回一个点对象,它是quad类的私有成员。我已经写了这个函数,它可以编译,但是我不知道调用这个函数的正确语法。
Point Quad::getA()
{
return Point a;
}
void Quad::check()
{
cout << this->getA(); //will not work
}
可以:
class Quad {
Point a;
Point getA();
void check();
};
Point Quad::getA()
{
return a;
// or: return Point();
// to return a new point
}
void Quad::check()
{
cout << getA(); // requires << be overloaded
// perhaps this is what you want:
// cout << getA().x;
}
return Point a;
错误。您可以创建一个对象然后返回,或者在返回语句中创建该对象。
正确的语法:
Point Quad::getA()
{
Point a;
return a;
}
Point Quad::getA()
{
return Point();
}
内部class
Point* p;
在GetA()函数中
if (p== null) {
p= new Point();
}
return p;
返回一个指针将是更好的解决方案。在程序的整个生命周期中保持单个指针将使开发人员更容易正确地销毁该指针。参考http://en.wikipedia.org/wiki/Singleton_pattern
对不起我的错。我有这个想法,但传达错了。
相关文章:
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 检查哪个对象调用了另一个对象的对象方法
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- 如何使用函数将一个对象的输入复制到另一个对象中
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 我重载了 << 和 = 运算符。为什么当我将一个对象分配给另一个对象并尝试打印它时,我会被打印出来?
- 打印对象的映射,其中另一个对象作为键
- 如何通过另一个对象中的命令正确地从一个对象返回数据
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 复制赋值函数如何访问另一个对象的私有成员(Stroustroup 原则和实践书)?
- 为什么数组中对象的析构函数在被另一个对象替换时不被调用?
- C++将带有重载构造函数的对象添加到另一个对象
- 如何将指针从unique_ptr传递到另一个对象并管理寿命
- 如何将"this"的生命周期移动到C++中的另一个对象中?
- 2 个 COM 对象,并在另一个对象中使用其中一个对象的接口
- 将列表的对象C++移动到另一个对象