Constructor, Copy Constructor and Stack Creation : C++ -


this question regarding function stack creation.

suppose create function fn(int a,char b) , call main fn(a,b) , in case when function called fn. stack created return address, stack pointer (etc) local variables , parameters created , on return destroyed.

i have few questions: 1) our parameterized constructor suppose

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

does constructor myclass(int a,char c) create function stack , create local variables a , c.

2) suppose calling reference : function fn(int* a,char* b) or fn(int& a, char& b) , calling our main fn(&a,&b) , fn(a,b) respectively , in case also, function stack created return address,sp etc. question that, local pointer or reference created on stack in case (i.e. creating local copy of pointer or reference point passed object). or no local copy of object created , original object pointed pointer or refence directly passed?

3) can overload function fn(int& a,char& b) , 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 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 pointer function " << endl;     cout << hex << "*a : " << << endl;     cout << hex << "*c : " << (int*) c << endl;      } */  void fn (int& a, char& c) {      cout << "\n\ninside call 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&)' 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&)' ambiguous trial.cpp:5: note: candidates are: void fn(int, char) trial.cpp:7: note:                 void fn(int&, char&) make: *** [trial] error 1 

does constructor myclass(int a,char c) create function stack , create local variables a , c

yes, function stack created a , c not local variables function stack, member variables , there lifetime not end end of constructor. remain alive throughout lifetime of class instance member are.

or no local copy of object created , original object pointed pointer or reference directly passed?

this known pass reference. 2 ways are:

  • passing address of object or
  • pass object reference

in either case copy of object not created. actual object can modified within function, in case 1 pointer in function points address of object being passed while in case 2 reference argument merely alias object being passed.

can overload function fn(int& a,char& b) , fn(int a,int b)?

no, cannot because compiler cannot understand function version intend call when call as:

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

i misread, fn(int& a,int& b) , fn(int a,int b) instead of fn(int& a,char& b) , fn(int a,int b).
ofcourse can. have distinct types , hence qualify valid overloaded functions.


Comments