473,791 Members | 3,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete operator on smart pointers.

I am implementing my custom smart pointer:

template <typename T>
class MySmartPtr
{
public:
MySmartPtr(T* aPointer)
{
mPointer = aPointer;
}

inline T* operator->() const {return mPointer; }

private:
T* mPointer;
};
It could be used in this way:

MySmartPtr<Aobj = new A();
obj->Method1();
int value = obj->Method2();

The operator-overload allows accessing the A methods as they are
accessed via a common pointer.

I want to allow the final user call the delete operator as in a common
pointer when he wants to explicitly remove the pointer:

delete obj;
There is a way I could implement an overload of the delete pointer in
my MySmartPtr<Tor there is some workaround on this?

Thanks in advance,
ernesto

Jan 6 '07 #1
10 2967
"Ernesto Bascón" <eb******@gmail .comschrieb im Newsbeitrag
news:11******** *************@s 80g2000cwa.goog legroups.com...
>I am implementing my custom smart pointer:

template <typename T>
class MySmartPtr
{
public:
MySmartPtr(T* aPointer)
{
mPointer = aPointer;
}

inline T* operator->() const {return mPointer; }

private:
T* mPointer;
};
It could be used in this way:

MySmartPtr<Aobj = new A();
obj->Method1();
int value = obj->Method2();

The operator-overload allows accessing the A methods as they are
accessed via a common pointer.

I want to allow the final user call the delete operator as in a common
pointer when he wants to explicitly remove the pointer:

delete obj;
There is a way I could implement an overload of the delete pointer in
my MySmartPtr<Tor there is some workaround on this?
To make your smart pointer really smart, it should also implement a default
constructor, a copy constructor and an assignment operator (actually two, a
copy assignment operator and one to assign raw pointers). If they are
implemented correctly, you could just write

obj = 0; // or NULL

to free whatever the smartpointer owned before.

HTH
Heinz
Jan 7 '07 #2

Ernesto Bascón wrote:
I am implementing my custom smart pointer:

template <typename T>
class MySmartPtr
{
public:
MySmartPtr(T* aPointer)
{
mPointer = aPointer;
}

inline T* operator->() const {return mPointer; }

private:
T* mPointer;
};
It could be used in this way:

MySmartPtr<Aobj = new A();
obj->Method1();
int value = obj->Method2();

The operator-overload allows accessing the A methods as they are
accessed via a common pointer.

I want to allow the final user call the delete operator as in a common
pointer when he wants to explicitly remove the pointer:

delete obj;
There is a way I could implement an overload of the delete pointer in
my MySmartPtr<Tor there is some workaround on this?

Thanks in advance,
ernesto
did you try debuggig and watch what object is actually in use then
delete it directly?

Jan 7 '07 #3

Heinz Ozwirk wrote:
>
To make your smart pointer really smart, it should also implement a default
constructor, a copy constructor and an assignment operator (actually two, a
copy assignment operator and one to assign raw pointers). If they are
implemented correctly, you could just write

obj = 0; // or NULL

to free whatever the smartpointer owned before.

HTH
Heinz
That's an elegant point!
I watched my google search carefully with my pair of glasses and I
turned out to have the same incorrect thought with others. hehe

Jan 7 '07 #4

Heinz Ozwirk wrote:
>
To make your smart pointer really smart, it should also implement a default
constructor, a copy constructor and an assignment operator (actually two, a
copy assignment operator and one to assign raw pointers). If they are
implemented correctly, you could just write

obj = 0; // or NULL

to free whatever the smartpointer owned before.

HTH
Heinz
That's an elegant point!
I watched my google search carefully with my pair of glasses and I
turned out to have the same incorrect thought with others. heh

Jan 7 '07 #5
Ernesto Bascón wrote:
MySmartPtr(T* aPointer){ mPointer = aPointer; }
Better do
MySmartPtr( T *const aPointer=0 )throw():mPoint er(aPointer){ }
MySmartPtr<Aobj = new A();
It can produce sequence

MySmartPtr<A>() + MySmartPtr<A>() + MySmartPtr<A>(c onst MySmartPtr<A>&)
or MySmartPtr<A>:: operator = (const MySmartPtr<A>&)

Better do
MySmartPtr<Aobj (new A() );
obj->Method1();
Define both
? T* operator-() ? throw(){ return mPointer; }
? T& operator* () ? throw(){ return *mPointer; }
be shure "const" instead "?" is correct for MySmartPtr behaviour
int value = obj->Method2();
I want to allow the final user call the delete operator as in a common
pointer when he wants to explicitly remove the pointer:

delete obj;
Try do
public:
operator T* ()throw(){ T *const tmp=mPointer; mPointer=0; return
tmp; }
~MySmartPtr()th row(){ delete mPointer; mPointer=0; }

Define (if you can not dynamic copy like this, else as need)
private:
MySmartPtr(cons t MySmartPtr&)thr ow():mPointer(0 ){ exit(1); }
void operator= (const MySmartPtr&)thr ow(){ exit(1); }

Jan 12 '07 #6
Grizlyk wrote:
Define (if you can not dynamic copy like this, else as need)
private:
MySmartPtr(cons t MySmartPtr&)thr ow():mPointer(0 ){ exit(1); }
void operator= (const MySmartPtr&)thr ow(){ exit(1); }
Yes, and yet
public:
T* operator= (T *const aPointer)throw( ) { delete mPointer;
mPointer=aPoint er; }

Jan 12 '07 #7
Ernesto Bascón wrote:
I am implementing my custom smart pointer:

template <typename T>
class MySmartPtr
{
public:
MySmartPtr(T* aPointer)
{
mPointer = aPointer;
}

inline T* operator->() const {return mPointer; }

private:
T* mPointer;
};
It could be used in this way:

MySmartPtr<Aobj = new A();
obj->Method1();
int value = obj->Method2();

The operator-overload allows accessing the A methods as they are
accessed via a common pointer.

I want to allow the final user call the delete operator as in a common
pointer when he wants to explicitly remove the pointer:

delete obj;
There is a way I could implement an overload of the delete pointer in
my MySmartPtr<Tor there is some workaround on this?
I presume there are other functions omitted from your code above, and
from your description, it's not clear what semantics you are intending
to give this smart pointer (though it sounds like you want shared
ownership). Generally, smart pointers (e.g., std::auto_ptr,
std::tr1::share d_ptr, boost::scoped_p tr, Loki::SmartPtr, and the smart
pointer in FAQ 16.22) are responsible for cleaning up after themselves
at the appropriate time so that the user doesn't have worry about
deleting anything. Indeed, that's often the primary point. What is your
point?

As for overloading delete, you probably don't want to do that because
it would change the semantics of delete from operating on the object
(viz. MySmartPtr<>) to operating on the contained object. In other
words, class-specific new/delete operators are for deleting the smart
pointer class itself:

MySmartPtr<T>* pT
= new MySmartPtr<T>( 0 ); // Calls MySmartPtr<T>:: operator new
delete pT; // Calls MySmartPtr<T>:: operator delete

You could accomplish your goal by supplying a cast to T*, but that
allows some other dangerous code. The preferred method is to supply a
reset function:

template<class T>
void MySmartPtr<T>:: Reset( T* const p = 0 )
{
delete mPointer;
mPointer = p;
}

It's best to use RAII and let the smart pointer do the dirty work. See
the FAQ mentioned above and those following, and see this chapter from
_Modern C++ Design_ on Loki's smart pointers:

http://www.informit.com/articles/pri...p?p=25264&rl=1

Cheers! --M

Jan 12 '07 #8
I presume there are other functions omitted from your code above, and
from your description, it's not clear what semantics you are intending
to give this smart pointer (though it sounds like you want shared
ownership). Generally, smart pointers (e.g., std::auto_ptr,
std::tr1::share d_ptr, boost::scoped_p tr, Loki::SmartPtr, and the smart
pointer in FAQ 16.22) are responsible for cleaning up after themselves
at the appropriate time so that the user doesn't have worry about
deleting anything. Indeed, that's often the primary point. What is your
point?
Primarily, I want to provide a hierarchy of smart pointers, providing
the basic implementation in something like AbstractPtr<T>; the basic
implementation should include null pointer checking on -and dangling
pointer avoiding.

I want to provide also a Ptr<T: public AbstractPtr<Tim plementation
that should have the same behavior that the standard pointers, but with
the null pointer checking. This smart pointer should not have any
semantics (thus, should provide mechanisms to release the pointee
object manually).

Jan 12 '07 #9

Ernesto Bascón wrote:
I want to allow the final user call the delete operator as in a common
pointer when he wants to explicitly remove the pointer:

delete obj;
No you don't.

Jan 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
27043
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object will call it's destructor when it is made a target of a delete".
18
9724
by: Joe Seigh | last post by:
Is there a good write on this. The textbooks I have fluff over on this? Specifically, I trying to dereference with 2 levels of type conversion not 1, i.e. X<T> -> z => Y<T> -> z => T* -> z and *X<T> => *Y<T> => *T The Y<T> conversion has to be done as an expression temp. It cannot be done
7
3570
by: Xamalek | last post by:
Greetings, I have a question. When using an STL container, does deleting a container of pointers also call delete on the (contained) pointers? For example, if I have (ignore the fluff, it is simply used to explain my issue) struct Proc {
16
2022
by: cppaddict | last post by:
Hi, I am deleting some objects created by new in my class destructor, and it is causing my application to error at runtime. The code below compiles ok, and also runs fine if I remove the body of the destructor. So I think I am somehow using delete incorrectly, but I'm not sure exaclty what I'm doing wrong. I'd apprecitate any clarification and suggestions for rewriting the below properly.
19
2608
by: Xavier Décoret | last post by:
Hi, I long thought one could not delete a const pointer but the following code compiles under g++ and visual net: class Dummy { public: Dummy() {} ~Dummy() {}
15
8245
by: Dilip | last post by:
I am aware that the C++ standard in its present form does not say anything about threads, however I do have a relevant question. I am working on Windows XP/VC++ 8.0. Is there a problem new'ing a bunch of objects from one thread and deleting them in another? I do something like: struct GenericPointerDeleter {
7
5128
by: Bas Nedermeijer | last post by:
Hello, i am having trouble to use the sort() function of a list<>. I cannot seem to overload the operator<(). The variables to compare are references, like Item* itemA; Item* itemB;
1
1632
by: Gonçalo Rodrigues | last post by:
Hi all, I am a little confused about the delete operator, so I have a question. Suppose we have something like class Base { public: void* operator new(std::size_t size); void operator delete(void* ptr);
4
1841
by: mail.dsp | last post by:
Suppose in a class we overload four operators: operator new operator delete operator new operator delete class Test{ public: void * operator new (size_t t){ cout<<"\nCalling... new";
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
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...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10156
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
9997
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...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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
6776
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();...
3
2916
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.