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