Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Smart Pointers with templates

Reply
Thread Tools

Smart Pointers with templates

 
 
Shiva
Guest
Posts: n/a
 
      12-01-2004
I saw C++ FAQ LITE on how to go about creating a smart pointer.

I tried my hand at creating a smart pointer using templates. I'm
thinking of creating just one Pointer class to create pointers for all
classes. Here's a code excerpt.

class Object {
//This is a bare bones definition for Object
//Not complete yet.
int _objIdentifier;
public:
Object() {
}
};

template<typename T>
class Pointer;

class MyClass : public virtual Object {
private:
friend Pointer<MyClass>;
MyClass() {
x = 0;
std::cout << "MyClass::Constructor" << endl;
}
public:
int x;
~MyClass() {
std::cout << "MyClass:estructor" << endl;
}
};

template<typename T>
class Pointer : public virtual Object {
private:
T *_data;
public:
Pointer() {
_data = new T;
}
~Pointer() {
delete _data;
}
T *operator->(void) {
return _data;
}
};

I think this works with the restriction that I will have to always use
the default constructor and that every class in my application should
create one and only private default constructor.

Am I missing something? What potential problems will I have if I
institutionalize this in our Architecture?
 
Reply With Quote
 
 
 
 
Kurt Krueckeberg
Guest
Posts: n/a
 
      12-01-2004

>I saw C++ FAQ LITE on how to go about creating a smart pointer.
>
> I tried my hand at creating a smart pointer using templates. I'm
> thinking of creating just one Pointer class to create pointers for all
> classes. Here's a code excerpt.
>> Am I missing something? What potential problems will I have if I

> institutionalize this in our Architecture?


You might look into the boost shared_ptr template. See
C:\boost_1_31_0\libs\smart_ptr\smart_ptr.htm


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Smart Pointers: Is there something similar to smart pointers in C? MotoK C Programming 59 09-15-2006 07:03 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments