如何在实际类中访问另一个类的对象的实例
How can I access the instance of an object of another class in my actual class?
我 #included 高分文件到我的头上。 现在我正在我的潜艇.cpp中创建它的对象,但无论如何我都无法访问它。当我尝试写"highscore."来向我展示它的一些方法时,它没有显示任何内容,并告诉我我之前声明的变量未使用。
Submarine::Submarine(QGraphicsItem* parent):QObject (),
QGraphicsPixmapItem (parent)
{
Highscore *highscore = new Highscore;
QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(die()));
timer->start(50);
}
void Submarine::doSomething()
{
highscore->increase(); (HERE)
我怎样才能在我的潜艇课程的方法中获得我的高分????我必须在头文件中做更多的事情吗?
构造函数中有内存泄漏:
Submarine::Submarine(QGraphicsItem* parent):QObject (),
QGraphicsPixmapItem (parent)
{
Highscore *highscore = new Highscore; // <-- Your problem is here
QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(die()));
timer->start(50);
} // <-- the highscore and timer pointers go out of scope here
在构造函数的末尾,指向 Highscore 实例的指针超出范围并丢失。 您需要将其保存在 Submarine 类的成员变量中,以便随后在 doSomething() 方法中使用它。 同样的问题适用于在构造函数主体中创建的 QTimer* 计时器指针。
相关文章:
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 检查哪个对象调用了另一个对象的对象方法
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- 如何使用函数将一个对象的输入复制到另一个对象中
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 我重载了 << 和 = 运算符。为什么当我将一个对象分配给另一个对象并尝试打印它时,我会被打印出来?
- 打印对象的映射,其中另一个对象作为键
- 如何通过另一个对象中的命令正确地从一个对象返回数据
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 复制赋值函数如何访问另一个对象的私有成员(Stroustroup 原则和实践书)?
- 为什么数组中对象的析构函数在被另一个对象替换时不被调用?
- C++将带有重载构造函数的对象添加到另一个对象
- 如何将指针从unique_ptr传递到另一个对象并管理寿命
- 如何将"this"的生命周期移动到C++中的另一个对象中?
- 2 个 COM 对象,并在另一个对象中使用其中一个对象的接口
- 将列表的对象C++移动到另一个对象