下面的代碼編譯,但在運行時提供了一個錯誤的連續內存塊: # include <iostream>
# include <string.h>
class A {
public:
A() {}
A (int id, char * t_name) {
_id = id ;
name = new char [ strlen (t_name) + 1 ] ;
所以我製作了一個模擬井字遊戲的程序,並用valgrind運行它,它說我有內存泄漏。什麼是造成這種泄漏,我該如何解決? 這裏是從Valgrind的輸出: ==15253==
==15253== HEAP SUMMARY:
==15253== in use at exit: 72,704 bytes in 1 blocks
==15253== total heap usage: 37 allo
我知道,當爲您提供隱式構造函數時,您的類成員將從左到右和從上到下進行初始化。換句話說,按照他們聲明的順序。然後,當類對象超出範圍時,所有成員都以相反的順序被破壞。但是,如果我必須自己銷燬成員,我是否必須按照列出的順序來執行操作?例如: struct Dog {};
struct Animal
{
Dog* dog1 = new Dog;
Dog* dog2 = new D
如果需要,可以通過參數的構造函數將參數傳遞給類。 class Test {
public function __construct($echo) {
echo $echo;
}
}
$test = new Test('hello'); // Echos "hello"
是否有任何方法將參數傳遞給__destruct? class Test {