473,668 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1773
On 2005-05-21, CodeCracker <sa********@gma il.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********@gma il.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********@gma il.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
2142
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
6
1992
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 explicitly call delete on these instances when i no longer need them?
7
2795
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 & theImageryTypeTrimmed & Server.URLEncode("&f=") ddlMyDropDownList.Items.Add(New ListItem("MyTextValue", theFullLink & "af")) Which puts out the following HTML: <option value="~/folder/page.aspx?param1=value1%26f%3daf">-
14
5923
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 because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary changed to "&" by a browser when or other software, when editing and uploading the file to the web...
13
2798
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 produced like: <title>my very long title blab la blab la Blabla bla </title> Importer also has got problems with it
6
78374
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:\\" & StrComputer & "\root\cimv2") returns a value System._ComObject of Type Object
5
588
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 following class as an example: class Test { public: static void print_message(char *s) { printf("%s\n", s); } void delete_me()
19
10745
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 there an existing adapter? Thanks, Daniel.
30
3812
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 the end of the app for example: class test { public: int x;
0
8462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8583
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8656
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5681
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4205
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2791
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1786
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.