问题描述
我对以下示例代码有几个问题:
I have a few questions on the example code below :
#include <iostream>
using namespace std;
class Example
{
int a;
public:
void setval(int x);
int addval(Example &ex);
};
void Example::setval(int x)
{
a=x;
}
int Example::addval(Example &ex)
{
int c;
c=ex.a+a;
return c;
}
int main()
{
Example obj1,obj2;
int sum;
obj1.setval(10);
obj2.setval(20);
sum=obj1.addval(obj2);
cout<<" the sum is :"<<sum<<endl;
}
Output :
$g++ -o main *.cpp
$main
the sum is :30
问题:
1>当行sum = obj1.addval(obj2);执行,函数堆栈是什么样的?
1.a>在通过obj1调用addval(obj2)时,obj1.addval的堆栈是否包含变量obj2.a的内存地址?
2>变量c的范围是多少?
3> ca成员是变量吗?
我尝试过:
如果我无法澄清我的问题,请说出来。
预先感谢您的回复。
问候!
Questions:
1> When the line sum=obj1.addval(obj2); executes, what does the function stack look like?
1.a> in the call to addval(obj2) by obj1, does the stack of obj1.addval contain the memory address of the variable obj2.a ?
2> What is the scope of the variable c ?
3> Is c a member variable ?
What I have tried:
If I have not been able to make my question clear, please say so.
Thanks in advance for your responses.
Regards!
推荐答案
问题:
1>当行sum = obj1.addval(obj2);执行,函数堆栈是什么样的?
1.a>在通过obj1调用addval(obj2)时,obj1.addval的堆栈是否包含变量obj2.a的内存地址?
2>变量c的范围是多少?
3> ca成员是变量吗?
我尝试过:
如果我无法澄清我的问题,请说出来。
提前感谢您的回复。
问候!
Questions:
1> When the line sum=obj1.addval(obj2); executes, what does the function stack look like?
1.a> in the call to addval(obj2) by obj1, does the stack of obj1.addval contain the memory address of the variable obj2.a ?
2> What is the scope of the variable c ?
3> Is c a member variable ?
What I have tried:
If I have not been able to make my question clear, please say so.
Thanks in advance for your responses.
Regards!
这篇关于当同一个类的两个对象在同一个成员变量上运行时,堆栈帧是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!