Smart Pointers with templates

S

Shiva

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::Destructor" << 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?
 
K

Kurt Krueckeberg

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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top