C++ References
Say I declare a variable like this:
int &Foo = *(new int);
How do you ''delete'' that var?
Gyzmo
==============
"Moo-moo moo"
- Hell Bovine (Diablo II)
Gyzmo=============================="Well is the world standard" - The Alchemist"Not in Canada!" - jonnyfish
No i was wondering how you delete a reference to a dynamically created object
Gyzmo
==============================
He who codes all night and codes all day
Lives to get a lousy paycheck someday....
Gyzmo
==============================
He who codes all night and codes all day
Lives to get a lousy paycheck someday....
Gyzmo=============================="Well is the world standard" - The Alchemist"Not in Canada!" - jonnyfish
Thanks, I think it works (no errors compiling )
EDIT: Or running
Gyzmo
==============================
He who codes all night and codes all day
Lives to get a lousy paycheck someday....
Edited by - Gyzmo on August 6, 2000 5:27:36 PM
EDIT: Or running
Gyzmo
==============================
He who codes all night and codes all day
Lives to get a lousy paycheck someday....
Edited by - Gyzmo on August 6, 2000 5:27:36 PM
Gyzmo=============================="Well is the world standard" - The Alchemist"Not in Canada!" - jonnyfish
Are you using this kind of thing, or just asking? A better way would be to just use a pointer in the first place. If you need the reference for anything specific you can create one from the pointer. Just because it works doesn''t mean you should use it. If you do use a pointer you can wrap it inside an auto_ptr so you can''t forget to delete it (and it''ll keep it safe if you decided to use exceptions) e.g.
std::auto_ptr<int> pFoo(new int);int& Foo = *pFoo.get();//no need to explicitly delete anything
I was just wondering, I have NEVER used references and it is unlikely I ever will, but I just can''t stand not knowing. It just sort of popped into my mind and I just had to ask
Gyzmo
==============================
He who codes all night and codes all day
Lives to get a lousy paycheck someday....
Gyzmo
==============================
He who codes all night and codes all day
Lives to get a lousy paycheck someday....
Gyzmo=============================="Well is the world standard" - The Alchemist"Not in Canada!" - jonnyfish
You cannot delete a reference to an object
The following:
delete &Foo
means call ''delete'' on the pointer to Foo
&Foo is a form of the address of operator, not a reference.
The most common use of references is passing arguments to functions/methods where you do not want to construct a temporary.
It is like passing a pointer in effeciency, but a C++ reference can never be NULL so it is safer.
classic example
class Foo
{
Foo& operator=(const Foo& rhs);
};
Why a const reference? - Because you do not want to allow modification of rhs.
Why does it return Foo& - so you can chain assignments
example:
Foo a, b, c;
a = b = c;
Just like you can do with an int or long or any builtin type.
Also, typically you would initialize a reference to a stack object or as a function parameter( as described above ) and would not usually initialize it to something that has been allocated on the heap
The following:
delete &Foo
means call ''delete'' on the pointer to Foo
&Foo is a form of the address of operator, not a reference.
The most common use of references is passing arguments to functions/methods where you do not want to construct a temporary.
It is like passing a pointer in effeciency, but a C++ reference can never be NULL so it is safer.
classic example
class Foo
{
Foo& operator=(const Foo& rhs);
};
Why a const reference? - Because you do not want to allow modification of rhs.
Why does it return Foo& - so you can chain assignments
example:
Foo a, b, c;
a = b = c;
Just like you can do with an int or long or any builtin type.
Also, typically you would initialize a reference to a stack object or as a function parameter( as described above ) and would not usually initialize it to something that has been allocated on the heap
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement