472,358 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

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 1678
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
6
by: R.Z. | last post by:
i'm using a class from some api that is said to automatically call its destructor when its out of scope and deallocate memory. i create instances of this class using "new" operator. do i have to...
7
by: DC Gringo | last post by:
I am having a bear of a time with setting a URL query string as a text value in a dropdownlist and Server.URLEncode does not seem to do its job. theFullLink = theLinkPrefix &...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
13
by: Ragnar | last post by:
Hi, 2 issues left with my tidy-work: 1) Tidy transforms a "&amp;" in the source-xml into a "&" in the tidied version. My XML-Importer cannot handle it 2) in a long <title>-string a wrap is...
6
by: active | last post by:
This works with Strict Off But not with Strict On Sometimes I can figure out what is needed by running it and using QuickWatch to see the type required but GetObject("winmgmts:\\" &...
5
by: WaterWalk | last post by:
Hello. The question about "deleting this inside the class's member function" is discussed many times in this group and in the "C++ FAQs". But I still have two more questions. 1. Take the...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.