我已经搜索过了,但我对这三个概念并不是很了解.我何时必须使用动态分配(在堆中)以及它的真正优势是什么?静态和堆栈有什么问题?我可以编写整个应用程序而无需在堆中分配变量吗?
我听说其他语言包含了"垃圾收集器",所以你不必担心内存.垃圾收集器做什么?
您可以自己操作内存,而不能使用此垃圾收集器吗?
有人告诉我这个声明:
int * asafe=new int;
Run Code Online (Sandbox Code Playgroud)
我有一个"指针指针".这是什么意思?它不同于:
asafe=new int;
Run Code Online (Sandbox Code Playgroud)
?
我上了这堂课
class Person {
public:
Person(const std::string& name, const std::string& email, const std::string& city)
: name(name), email(email), city(city) {
}
bool hasCity() const {
return city.compare("") == 0;
}
void print() const {
std::cout << name + " <" + email + ">";
if(hasCity()){
std::cout << ", " + city;
}
std::cout << std::endl;
}
bool equalTo(const Person& comparedPerson) const {
return email.compare(comparedPerson.email) != 0;
}
bool equalId(std::string comparedId){
return email.compare(comparedId) != 0;
}
const std::string name;
const std::string email; …Run Code Online (Sandbox Code Playgroud)