程序执行过程中最重要的是函数调用
但是C++不像java,简单好学,复杂类型都是指针引用
写程序的都要会C++ 只有理解了C++/C ,才能懂计算机 ,只有理解了计算机,才能程序少出bug
C++的函数调用可以分三步

#include <iostream>
#include <string>
using namespace  std;
class Fa{
public:
    int a;
    int b;
    string c;
    Fa(const Fa& fa){
        cout<<"copy constructor called"<<endl;
        a = fa.a;
        b =fa.b+1;
        c = fa.c;
        cout<<"b:"<<this->b<<endl;
    }
    ~Fa(){
        cout<<"b:"<<this->b<<endl;
        cout<<"deconstractor called"<<endl;
    }
    Fa(){
    cout<<"b:"<<this->b<<endl;
    cout<<"constractor called"<<endl;
    }
};

Fa Process(Fa);

int main(){
    Fa fa;
    Fa fb=Process(fa);
    cout<<fb.b<<endl;
}

Fa Process(Fa fc){
    return fc;
}

这是一段简单的不能在简单的代码 就用它来解释
int main(){
整一个fa对象出来
Fa fa;
这一步执行的是 Fa fc(fa) 这就是函数入栈的过程 ,应为这是值传递 所以要经过拷贝构造函数
第二步 就是return fc 就要把Fa fb(fc) 这一步就是把函数的返回值保存至寄存器,并且 传给fb ,是不是和其他语言不一样
其实其他语言只是帮我们简化了这了流程 但是如果做C++不理解的话 含有指针的类对象 经过浅拷贝 如果一旦delete 回收 掉,那么就会出现野指针 程序就崩溃了

_____________________

Fa Process(Fa fc){
return fc;
}


Fa fb=Process(fa);
cout<<fb.b<<endl;

}
简单代码看懂C++函数调用机制
这个地方通过一个b值就可以看出
process函数 栈被回收,fc立即回收,所以就是中间的打印值就是1

相关文章:

  • 2021-07-30
  • 2021-10-10
  • 2021-08-29
  • 2022-01-05
  • 2022-12-23
  • 2022-01-02
  • 2021-07-07
猜你喜欢
  • 2021-11-16
  • 2021-06-09
  • 2021-10-16
  • 2021-08-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案