如何在 c++ 中输出指针对象的属性(具有 2 个或更多属性)
How do i output properties of a pointer object in c++ (with 2 or more properties)
Class A
{
public:
A();
A(int x , int y);
Private:
int x;
int y;
}
Class B
{
public:
B();
A getApointerobject() const;
Private:
A *APointerObject;
int main()
{
B bObj;
cout << bObj.getApointerobject(); //i overloaded the << so that i can //output B objects but it crushes
}
//Class B implementation (This is where i struggle)
A getApointerobject() const {
return *getApointerobject;
}
在 B::getApointerobject() 中,你递归地调用它。您应该更改为
A getApointerobject() const {
return *APointerObject;
}
在调用 getApointerobject() 方法之前是否初始化了 B::APointerObject?
getApointerobject
中,您尝试返回方法getApointerobject
本身的地址。我猜你的代码现在甚至没有编译?
我猜你想归还你的APointerObject
A B::getApointerobject() const {
return *this->APointerObject;
}
但请注意:内置类型(包括简单指针)没有默认构造函数。因此,由于您没有在class B
构造函数中初始化APointerObject
,因此您将使用野生指针。这意味着您的程序会在运行时崩溃或更糟(未定义的行为)
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++调用具有 *this 属性的单个帮助程序函数
- 具有常量属性的不可变类
- 如何在不知道属性具有哪些构造函数的情况下初始化属性?
- 类对象属性是否可以调用返回属性应具有的值的方法C++?
- 将具有 xml 属性的子树添加到提升属性树
- 使用类属性调用具有非类型模板参数的模板函数
- 输出(用于 GraphViz)提升图顶点及其属性,使用具有私有变量的类作为捆绑属性
- 我可以将单个顶点索引与具有所有属性(位置、法线、texCoord)的顶点缓冲区一起使用吗?
- 子类中具有不同值的静态基类属性
- 有没有办法将 vector<unsigned char> 插入 postgresql 表中,具有 bytea 属性,没有 UTF8 编码错误?
- LNK2019与另一个工作项目具有相同属性的项目上的错误
- 具有多个属性的预定义运算符
- 编写多个 protobuf 消息:1 条具有 1 个重复属性的消息或编写不同的消息
- 具有相似属性的不同数据类型的模板
- 可以 main() 及其参数具有属性
- 哪些 gnu 属性具有 c++11 语法
- 查找对象向量中属性具有相同值的对象
- 迭代向量中属性具有特定值的元素范围
- 如何在 c++ 中输出指针对象的属性(具有 2 个或更多属性)