#include<iostream>
#include <string>
using namespace std;
class Human
{
private:
int *age;
string *name;
public:
Human(string p_name, int value)
{
*name = p_name;
*age = value;
cout <<"Name of Person is "<<*name <<" and age is "<<*age<<endl;
}
~Human()
{
delete name;
delete age;
cout<<"Destructor release all memory So now name is "<<*name << " and age is "<<*age<<endl;
}
void display()
{
cout << "The name is "<<*name <<" and age is "<<*age<<endl;
}
};
int main()
{
int age = 24;
string name = "CODY";
Human cody(name,age);
cody.display();
}
它沒有列印任何東西....發生了什么,有人可以解釋一下嗎....是不是因為我錯誤地實作了指標變數...為什么它不正確,然后告訴我什么是替代解決方案
uj5u.com熱心網友回復:
為什么私有類中的指標變數不能指向類的外部變數
你問題的前提是錯誤的。私有成員變數可以指向類外部。
Human(string p_name, int value) { *name = p_name; *age = value;
您沒有初始化指標,因此通過它們間接導致未定義的行為。不要這樣做。
cout << "The name is "<<*name <<" and age is "<<*age<<endl;
在這里,您間接通過無效指標,程式的行為是未定義的。
delete name; delete age; cout<<"Destructor release all memory So now name is "<<*name << " and age is "<<*age<<endl;
即使指標不是無效的,洗掉它們也是錯誤的。您沒有使用 allocating 創建任何物件new,因此沒有什么可洗掉的。洗掉未通過分配回傳的指標new將導致未定義的行為。不要這樣做。
此外,即使delete是有效的,這也會使指標無效,并且通過cout陳述句中新無效的指標的連續間接將導致未定義的行為。也不要這樣做。
解決方案:洗掉delete線。
類指向類外變數的解決方案:為了參考函式外的物件,您需要使用間接。例如,您可以使用指標引數,并初始化成員以指向相同的物件:
Human(string* p_name, int* value)
: name(p_name), age(value)
{}
// example usage:
Human cody(&name, &age);
請注意,將指標存盤在成員中是不穩定的,因為您必須了解指向物件的相對生命周期以及包含指標的物件。您必須確保指標不會超過指向物件的壽命。
考慮到你的類的參考設計的問題,我敦促你考慮使用值來代替。一般來說,這可能是更有用的類:
struct Human {
std::string name;
int age;
void display()
{
std::cout
<< "The name is "
<< name
<< " and age is "
<< age
<< '\n';
}
};
int main()
{
Human cody {
.name="CODY",
.age=24,
};
cody.display();
}
uj5u.com熱心網友回復:
您給定的程式中有幾個錯誤,如下所述。
錯誤 1
首先你需要確保資料成員age和name指向一些適當型別的變數。然后只有你可以取消參考它們。由于您要取消參考不指向適當型別物件的指標,因此您的程式中有未定義的行為。
Human(string p_name, int value)
{
*name = p_name; //Undefined behavior because name doesn't point to an std::string object
*age = value; //Undefined behavior because age doesn't point to an int object
cout <<"Name of Person is "<<*name <<" and age is "<<*age<<endl;
}
錯誤 2
其次,您可以delete在使用new. 并且由于您沒有分配任何記憶體 using new,因此使用不正確delete。
~Human()
{
delete name; //not valid
delete age; //not valid
cout<<"Destructor release all memory So now name is "<<*name << " and age is "<<*age<<endl; //undefined behavior as explained in mistake 3
}
錯誤 3
第三,您正在取消參考指標age,并且name在您使用delete它們之后。這再次導致未定義的行為。
cout<<"Destructor release all memory So now name is "<<*name << " and age is "<<*age<<endl; //undefined behavior because you just used `delete` on the pointers and also because they don't point to objects of the appropriate type
最好使用智能指標來為您處理記憶體管理。也就是說,當我們使用智能指標時,我們不必手動使用newand delete。
為了讓您了解如何糾正當前的 2 個錯誤,您可以參考下面給出的修改示例:
//this uses constructor initializer list
Human(string p_name, int value): age(new int(value)), name(new std::string(p_name))
{
cout <<"Name of Person is "<<*name <<" and age is "<<*age<<endl;
}
//destructor
~Human()
{
if (name != nullptr && age!=nullptr)
{ //check that
delete name;
delete age;
cout<<"delete used successfully"<<endl;
}
else
{
cout<<"delete not used"<<endl;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/444619.html
