- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
foo
函数完成后,myClass
指针会发生什么变化?是否自动删除?
bar
函数完成后,myThread
指针会发生什么变化? (假设myThread
指向一个QThread
对象)
void foo()
{
MyClass *myClass = new MyClass();
myClass->doSomething();
}
void bar()
{
// Suppose that MyThread is a QThread class
MyThread* myThread = new MyThread(2.5);
// Connect the Thread to get the result
connect(myThread, SIGNAL(sendResult(double)), this, SLOT(getResult(double)));
// Start the thread
myThread->start();
}
提前致谢
最佳答案
你在这里使用 C++,如果你不这样做,没有人会删除你的对象。您编写的每个 new
都需要您编写一个 delete
来释放内存(就像在 C 中每个 malloc
都需要一个 free
).
只有对象被删除:
void foo()
{
MyClass myClass;
myClass.doSomething();
}
然后 MyClass
的析构函数在 foo 返回时被调用。一般来说,除非您需要对象在您的范围之外持久化,否则优先选择对象而不是指针,这将防止代码中的内存泄漏。
要考虑的特殊情况:
注意:对于QThread,完成后应该要求删除。参见 When or how to delete QThread in Qt .
关于c++ - 关于C++指针的疑惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42122496/
我有以下变量: string str1 = "1"; string str2 = "asd"; string str3 = "3.5"; string str4 = "a"; 现在我需要找到每个字符串
这个问题在这里已经有了答案: Is Java "pass-by-reference" or "pass-by-value"? (92 个回答) 关闭 8 年前。 public class progr
我对这种行为有些疑惑。谁能解释一下 void Decrement(int* x){ *x--; //using this line, the output is 5
您好 StackOverflow 用户(或 Stackoverflowers?): 我正在通过编写 WPF 代码来学习。我阅读了几篇文章/看到了几个截屏视频,并且来自 WEB 开发背景,我启动了 VS
在阅读 HTML5 IndexedDB Specification 时我对它的异步请求模型有些怀疑。查看 request api example 时, open 方法用于启动异步请求。 var req
有几个问题,首先是这个方法,它将 int[] 转换为 byte[]: public static byte[] intToByte(int[] input){ ByteBuffer byteB
我是一名优秀的程序员,十分优秀!