473,785 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Smart pointer implementation.

Hello,
I have tried this smart pointer implementation, but it is not working and
I am not able to figure out why .......Also, can you please suggest more
effective way/s of doing the same ???
Thank you,
Maadhuu.

//Smart.h

#ifndef _SMART_H
#define _SMART_H
#include<iostre am>

using namespace std;

template<typena me T>
struct PointerToT
{
explicit PointerToT<T>(T * realPtr=0):poin tee(realPtr) {cout << "in
constructor";}
PointerToT(Poin terToT& rhs)
{
pointee = rhs.pointee;
rhs.pointee = 0;
}
PointerToT<T>& operator=(Point erToT<T>& that)
{
if(this == &that) return *this;
delete pointee ;
pointee = that.pointee;
delete that.pointee ;
return *this;

}
~PointerToT<T>( ) { delete pointee;}

T& operator*() const { return *pointee;}
T* operator->() const { return &**this;} // &(this->operator*()) -
&(*this).operat or*() -
//& (* *this);

private:
T* pointee;
};

#endif //_SMART_H

//smart.cpp

#include<iostre am>
#include "smart.h"
using namespace std;

void printNode(ostre am &os,const PointerToT<char >& ptr)
{
os << *ptr;
}
int main()
{
char *p = "abcdef";
cout << p;
PointerToT<char > ptr(p); //not working.

printNode(cout, ptr);

PointerToT<char > ptr1(ptr);
PointerToT<char > ptr2;
ptr2 = ptr1;

return 0;
}

Thank You once again.

Dec 3 '05 #1
3 2301
maadhuu wrote:
I have tried this smart pointer implementation, but it is not working and
I am not able to figure out why .......Also, can you please suggest more
effective way/s of doing the same ???


Sure: #include <boost/shared_ptr.hpp>

Time saving and effective! ;-)

Regards,
Matthias
Dec 3 '05 #2

maadhuu wrote:
Hello,
I have tried this smart pointer implementation, but it is not working and
I am not able to figure out why .......Also, can you please suggest more
effective way/s of doing the same ???


Donot reinvent the wheel. Look up auto_ptr. (They are not the "all
purpose" smart pointers, but they will suffice your needs as indicated
from this program)

Anyways, your program is working the way it is expected to work ;-)

Dec 3 '05 #3
maadhuu wrote:
Hello,
I have tried this smart pointer implementation, but it is not working and
I am not able to figure out why .......Also, can you please suggest more
effective way/s of doing the same ???
Thank you,
Maadhuu.

//Smart.h

#ifndef _SMART_H
#define _SMART_H
#include<iostre am>

using namespace std;
Don't do that in a header.

http://www.parashift.com/c++-faq-lit....html#faq-27.5
template<typena me T>
struct PointerToT
{
explicit PointerToT<T>(T * realPtr=0):poin tee(realPtr) {cout << "in
constructor";}
Try to be careful with line breaks when posting.
PointerToT(Poin terToT& rhs)
{
pointee = rhs.pointee;
Why not initialization?
rhs.pointee = 0;
}
PointerToT<T>& operator=(Point erToT<T>& that)
{
if(this == &that) return *this;
delete pointee ;
pointee = that.pointee;
delete that.pointee ;
Don't these two last lines ring a bell?
return *this;

}
~PointerToT<T>( ) { delete pointee;}

T& operator*() const { return *pointee;}
T* operator->() const { return &**this;} // &(this->operator*()) -
&(*this).operat or*() -
//& (* *this);
What's all that? Check what you post!
private:
T* pointee;
};

#endif //_SMART_H

//smart.cpp

#include<iostre am>
#include "smart.h"
using namespace std;

void printNode(ostre am &os,const PointerToT<char >& ptr)
{
os << *ptr;
}
int main()
{
char *p = "abcdef";
cout << p;
PointerToT<char > ptr(p); //not working.
This should work ok.
printNode(cout, ptr);

PointerToT<char > ptr1(ptr);
PointerToT<char > ptr2;
ptr2 = ptr1;

return 0;
Now, assuming you corrected the operator= above, this will try to
delete something that was not allocated with new. This whole code is
equivalent to

delete "abcdef";

which is clearly not legal. Smart pointers are meant to be used with
dynamically allocated memory!
}

Thank You once again.


And what about std::auto_ptr?
Jonathan

Dec 3 '05 #4

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

Similar topics

24
2204
by: Christopher Benson-Manica | last post by:
Is there anything wrong with my attempt (below) at implementing something resembling a smart pointer? template < class T > class SmartPointer { private: T *t; public:
6
2310
by: zl2k | last post by:
hi, When I considered about preventing memory leaking, the method came up to my mind is using boost smart pointer if possible (use stl::vector instead of type, use smart pointer whenever declare an instance of class). With the container of STL, this may often result in a vector of smart pointers, or a smart pointer of STL container. Am I making things too complicated or this is an usual implementation? Thanks in advance. zl2k
92
5124
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
14
18210
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun to work in C# and am asking for suggestions/comments of how to implement a similar data sharing technique in C#. A C++ smart pointer can be used to share common information. For example, assume information managed by objects I1, I2, I3,...
33
5083
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
10
2967
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
54
12020
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining the advantages of smart pointers endlessly (which are currently used in all our C++ software; we use the Boost smart pointers) as I'm seriously concerned that there is a shift to raw pointers. We are not developing system software but rather...
3
5392
by: alebcn75 | last post by:
Hello, I'm having a design issue that I can't solve. I'm getting from a network a stream of data, which is a http message (my program is neither a client or server, it just catches messages over the network) For this I have a class http_message with the usual methods (to access header, fields, etc) I also have two other classes, http_request and http_response, which should be used depending on the type of message received. These two
13
2079
by: Phil Bouchard | last post by:
I am currently writting a smart pointer which is reasonnably stable and I decided supporting allocators for completion because of its increase in efficiency when the same pool used by containers is shared. I was told the new standards are being finalized and I am hoping minor but important changes could be applied before anything else. To give a quick overview on the current status of this topic, you will find below the latest text...
50
4515
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
9645
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
10147
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
10090
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
8971
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
7499
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
6739
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
5380
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
4050
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
3
2879
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.