Sep 30, 2011 at 6:30am UTC
First:
which one can I use to call the copy constructor?
Class name is Student;
Student obj;
Student *obj_p = new Student;
Student stu(obj);
Student stu(*obj_p);
if not, what can I do to pass a point to class to copy?
Second:
Does reference point to stack or heap?
Can a reference point to a variable in stack or heap? Ex:
1)int *p = new int(25);
int &t = *p;
2)
int p = 10;
int &t = p;
3) if I can define the reference in heap(stash), how can I release it?
Is it like this: delete &t; (t is the reference in(2) above)
Thank you for your help!
Last edited on Sep 30, 2011 at 6:49am UTC
Sep 30, 2011 at 7:04am UTC
int *p = new int(25);
int &t = *p;
Is this method valid?
Sep 30, 2011 at 7:19am UTC
Thanks a lot! Because I want to pass a object as reference argument and release it in the function
Sep 30, 2011 at 7:36am UTC
You can do that by taking the address of the referenced object, i.e. delete &ref;
But that's a bad idea no matter how you look at it. What you have there is called a sink (a function that takes ownership of an object) and you should pass a unique_ptr (or auto_ptr in C++03).
Last edited on Sep 30, 2011 at 7:36am UTC