469,887 Members | 1,201 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,887 developers. It's quick & easy.

can I do "delete &reference"

class A {
public:
A(obj &objRef): mObj(objRef) {
}
~A() {
delete &mObj;
}
private:
Obj &mObj;
}

Can I do what I did in ~A() ?

Jul 23 '05 #1
6 1588
On 2005-05-21, CodeCracker <sa********@gmail.com> wrote:
class A {
public:
A(obj &objRef): mObj(objRef) {
}
~A() {
delete &mObj;
}
private:
Obj &mObj;
}

Can I do what I did in ~A() ?


Yes, but you probably shouldn't

cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #2

CodeCracker wrote:
class A {
public:
A(obj &objRef): mObj(objRef) {
}
~A() {
delete &mObj;
}
private:
Obj &mObj;
}

Can I do what I did in ~A() ?


Can you make sure that your object has been allocated on heap rather
than on stack?

--
Imanpreet Singh Arora

Jul 23 '05 #3
Minti wrote:

CodeCracker wrote:
Obj &mObj;

Can you make sure that your object has been allocated on heap rather
than on stack?


You also can't tell if a pointer points to an object on the heap or stack.

From the top: Prefer weaker constructions over constructions with wider
interfaces. Prefer int unless you need double, prefer std::string unless you
need the freedom of char[], and prefer references unless you need one of
these:

1. reseat to something else
2. index with [] (essentially the same as 1)
3. could be NULL
4. a compilable comment

Despite item 3, you should not pass NULL into object interfaces (you should
pass handles to Null Object pattern objects, at least). Items 1 and 2 imply
a method should not take a pointer argument and point it to something else
as a convenience.

Item 4 is the clicker. An interface should request a pointer (or better yet,
a smart pointer) if it documents that it will own the pointee, and will
delete it. Passing such a pointer symbolizes transfer of ownership.

Without item 4, the other items indicate you should pass a reference even if
you intend to delete the referand from the heap. So, like the others said,
don't.

Now research smart pointers, to learn to encapsulate the ownership from the
clients of a heap object.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #4
Donovan Rebbechi wrote:
On 2005-05-21, CodeCracker <sa********@gmail.com> wrote:
class A {
public:
A(obj &objRef): mObj(objRef) {
}
~A() {
delete &mObj;
}
private:
Obj &mObj;
}

Can I do what I did in ~A() ?


Yes, but you probably shouldn't


You can also write:

int i;
delete &i;

.... but you probably shouldn't :-)

Jul 23 '05 #5
Minti wrote:
CodeCracker wrote:
class A {
public:
A(obj &objRef): mObj(objRef) {
}
~A() {
delete &mObj;
}
private:
Obj &mObj;
}

Can I do what I did in ~A() ?

Can you make sure that your object has been allocated on heap rather
than on stack?


Even that isn't good enough. mObj is NOT allocated with new,
it's undefined behavior to free it with delete.
Jul 23 '05 #6
CodeCracker <sa********@gmail.com> wrote:
class A {
public:
A(obj &objRef): mObj(objRef) {
}
You would have to write
A(obj &objRef): mObj(*(new obj(objRef))) {
}
~A() {
delete &mObj;
}
private:
Obj &mObj;
}

Can I do what I did in ~A() ?

Cheers
Maett
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by solosnake | last post: by
6 posts views Thread by R.Z. | last post: by
7 posts views Thread by DC Gringo | last post: by
14 posts views Thread by Arne | last post: by
5 posts views Thread by WaterWalk | last post: by
19 posts views Thread by Daniel Pitts | last post: by
30 posts views Thread by Medvedev | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.