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: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?
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?