构造函数,复制构造函数和堆栈创建:C ++ [英] Constructor, Copy Constructor and Stack Creation : C++

查看:179
本文介绍了构造函数,复制构造函数和堆栈创建:C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于函数栈的创建。

This question is regarding Function Stack Creation.

假设我们创建一个函数 fn(int a,char b),并从main fn(A,B)调用,在这种情况下,函数被称为fn。使用返回地址,堆栈指针(etc)创建堆栈,其中创建局部变量和参数,并且在返回时销毁。

Suppose we create a function fn(int a,char b) and call from main fn(A,B) , in this case when the function is called a fn. stack is created with return address, Stack pointer (etc) where local variables and parameters are created and on return is destroyed.

我有几个问题:
1)对于我们的参数化构造函数,假设

I have a few questions: 1) For our parameterized constructor suppose

myClass{
    int a;
    char c;
public:
    myClass(int a,char c)
    {
        this->a=a;
        this->c=c;
    }
};

构造函数 myClass(int a,char c)还创建其函数栈并创建局部变量 a c

does the constructor myClass(int a,char c) also create its function stack and create local variables a and c.

2)现在假设我们通过引用调用:我的函数是 fn(int * a,char * b) fn(& a,char& b)并从 fn(& A,& B) fn(A,B),在这种情况下,也会创建一个带返回地址,SP等的函数堆栈。我的问题是,将创建一个局部指针或引用在这种情况下(即创建一个指针或引用的本地副本,将指向传递的对象)。或者是没有创建对象的本地副本,并且指针或refence指向的原始对象直接传递?

2) Now suppose we are calling by reference : my function is fn(int* a,char* b) or fn(int& a, char& b) and calling from our main by fn(&A,&B) and fn(A,B) respectively , in this case also, a function stack will be created with return address,SP etc. My question is that, will a local pointer or reference be created on stack in this case (i.e. creating a local copy of pointer or reference that will point to the passed object). Or is it that no local copy of object is created and the original object pointed by the pointer or the refence is directly passed?

3)我们可以重载一个函数, code> fn(int& a,char& b)和 fn(int a,int b)

3) Can we overload a function like fn(int& a,char& b) and fn(int a,int b)?

感谢

EDIT

#include <iostream>

using namespace std;

void fn(int , char);
//void fn (int* a, char* c);
void fn (int& a, char& c);

int main()
{
   int a=10;
   char c= 'c';


  cout << "Inside main()" << endl;
  cout << hex << "&a : " << &a << endl;
  cout << hex << "&c : " << (int *)&c << endl;

   fn(a,c);
   //fn(&a,&c);
   fn(a,c);

    return 0;
    }


void fn (int a, char c)
{
    int tempInt;
    char tempChar;
    cout << "\n\nInside Call By Value Function " << endl;
    cout << hex << "&a : " << &a << endl;
    cout << hex << "&c : " << (int *)&c << endl;
    cout << hex << "&tempInt : " << &tempInt << endl;
    cout << hex << "&tempChar : " << (int *)&tempChar << endl;
    }

/*void fn (int* a, char* c)
{

    cout << "\n\nInside Call By Pointer Function " << endl;
    cout << hex << "*a : " << a << endl;
    cout << hex << "*c : " << (int*) c << endl;

    }
*/

void fn (int& a, char& c)
{

    cout << "\n\nInside Call By Reference Function " << endl;
    cout << hex << "*a : " << &a << endl;
    cout << hex << "*c : " << (int*) &c << endl;

    }

输出:

$ make
g++ -Wall Trial.cpp -o Trial
Trial.cpp: In function `int main()':
Trial.cpp:19: error: call of overloaded `fn(int&, char&)' is ambiguous
Trial.cpp:5: note: candidates are: void fn(int, char)
Trial.cpp:7: note:                 void fn(int&, char&)
Trial.cpp:21: error: call of overloaded `fn(int&, char&)' is ambiguous
Trial.cpp:5: note: candidates are: void fn(int, char)
Trial.cpp:7: note:                 void fn(int&, char&)
make: *** [Trial] Error 1


推荐答案


构造函数 myClass(int a,char c)也创建它的函数栈并创建局部变量 a c

does the constructor myClass(int a,char c) also create its function stack and create local variables a and c

是的,创建一个函数栈,但 a c 不是函数堆栈的局部变量,它们是成员变量,并且生命周期不以构造函数结束。

Yes, a function stack is created but a and c are not local variables to the function stack, they are member variables and there lifetime does not end with the end of the constructor. They remain alive throughout the lifetime of the class instance whose member they are.


或者,没有对象的本地副本是对象的本地副本

这被称为传递引用。两种方法是:

This is known as pass by reference. The two ways are:


  • 传递对象或
  • 的地址
  • 将对象传递参考

  • Passing the address of the object or
  • Pass the object by a reference

在任何一种情况下,都不会创建对象的副本。实际的对象可以在函数内修改。如果 1 ,函数中的指针指向要传递的对象的地址,而 2 引用参数只是要传递的对象的别名。

In either case the copy of the object is not created. The actual object can be modified within the function, In case 1 the pointer in the function points to the address of the object being passed while in case 2 the reference argument is merely an alias to the object being passed.


fn(int& a,char& b) fn(int a,int b)? >

不,因为编译器不能理解你打算调用哪个函数版本时, / p>

No, you cannot because the compiler cannot understand which function version you intend to call when you call it as:

int i = 10;
int j = 20;
fn(i,j);

fn(int& a,int& b) fn(int a,int b) code> fn(int& a,char& b) fn(int a,int b)

当然可以。它们具有不同的类型,因此有资格作为有效的重载函数。

I misread, as fn(int& a,int& b) and fn(int a,int b) instead of fn(int& a,char& b) and fn(int a,int b).
Ofcourse you can. They have distinct types and hence qualify as valid overloaded functions.

这篇关于构造函数,复制构造函数和堆栈创建:C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆