Two questions about copy constructor

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
Does reference point to stack or heap?

It's an alias for the object it has been initialized with.

Can a reference point to a variable in stack or heap?

Of course.

if I can define the reference in heap(stash), how can I release it?

You can't release it, nor is it necessary. Why do you think that?
int *p = new int(25);
int &t = *p;

Is this method valid?
Yes.
Thanks a lot! Because I want to pass a object as reference argument and release it in the function
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
Topic archived. No new replies allowed.