class 参数是否在函数执行后存储?
Are class arguments stored after function execution?
我是 C++ 的新手,正在努力更好地理解如何在 class 方法中分配和存储内存。
假设我有以下代码:
#include <iostream>
using namespace std;
class ExampleClass {
public:
void SetStoredAttribute(int Argument);
int GetStoredAttribute(void);
private:
int StoredAttribute;
};
void ExampleClass::SetStoredAttribute(int Argument) {
StoredAttribute = Argument;
};
int ExampleClass::GetStoredAttribute(void) {
return StoredAttribute;
};
int main() {
ExampleClass Object;
Object.SetStoredAttribute(1);
cout << Object.GetStoredAttribute();
return 0;
};
我的 SetStoredAttribute 方法完成后,C++ 会如何处理参数?它会释放内存还是我需要手动执行此操作?
函数参数,包括成员函数中的参数,总是有 automatic storage duration。它们在函数作用域的开头开始存在,在函数作用域的末尾停止存在,实现处理确保它们被“分配”和“解除分配”。
在大多数平台上,这是通过两种方式实现的。
- 首先是一个堆栈,通常称为堆栈,因为它是执行线程所特有的,在CPU中有一个特殊用途的寄存器指向顶.
- 其次,参数直接赋值给CPU中的寄存器。
我是 C++ 的新手,正在努力更好地理解如何在 class 方法中分配和存储内存。
假设我有以下代码:
#include <iostream>
using namespace std;
class ExampleClass {
public:
void SetStoredAttribute(int Argument);
int GetStoredAttribute(void);
private:
int StoredAttribute;
};
void ExampleClass::SetStoredAttribute(int Argument) {
StoredAttribute = Argument;
};
int ExampleClass::GetStoredAttribute(void) {
return StoredAttribute;
};
int main() {
ExampleClass Object;
Object.SetStoredAttribute(1);
cout << Object.GetStoredAttribute();
return 0;
};
我的 SetStoredAttribute 方法完成后,C++ 会如何处理参数?它会释放内存还是我需要手动执行此操作?
函数参数,包括成员函数中的参数,总是有 automatic storage duration。它们在函数作用域的开头开始存在,在函数作用域的末尾停止存在,实现处理确保它们被“分配”和“解除分配”。
在大多数平台上,这是通过两种方式实现的。
- 首先是一个堆栈,通常称为堆栈,因为它是执行线程所特有的,在CPU中有一个特殊用途的寄存器指向顶.
- 其次,参数直接赋值给CPU中的寄存器。