Constructor, Copy Constructor and Stack Creation : C++

匿名 (未验证) 提交于 2019-12-03 01:09:02

问题:

This question is regarding Function Stack Creation.

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.

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;     } }; 

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

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) Can we overload a function like fn(int& a,char& b) and fn(int a,int b)?

Thanks

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;      } 

Output:

$ 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 

回答1:

does the constructor myClass(int a,char c) also create its function stack and create local variables a and 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.

Or is it that no local copy of object is created and the original object pointed by the pointer or the reference is directly passed?

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

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

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.

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

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); 

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.



回答2:

To begin with, your concept is slightly incorrect.

i.e. Stack is not created with a function call. Rather each thread of execution has its own stack already. It is even there when a single main is called. However an ACTIVATION record is pushed on the stack when a function is called. And the same is popped when returning from a function.

So for

  1. Stack is already there and an activation record is pushed on the stack for each function call. The variables live throughout the lifetime of the object.

  2. If your function takes a pointer as argument (i.e. call by reference), there will be a pointer variable pushed on the stack which is passed the address of the original variable. The original variable remains intact and modifying its value via pointer would change the original variable.

  3. You can only overload functions when their signatures are different. This means type, number or order of parameters. In the e.g. you quoted, it is not possible to differentiate whether a passed int is a variable or it is an address of a variable. Hence this overload won't work.

EDIT: Point 3 above has as slight mistake. The e.g. quoted in question has second parameter different and hence qualifies as valid overload. Note that the criteria is not just the name of data type (i.e. int vs. int * is also different types), but rather the fact that given an input value, compiler should be able to differentiate and chose which overloaded version to call.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!