M
mario.rossi
Hello!
I would like to write a pointer to type template that behaves exactly and
completely like normal pointers, which I will use as starting point for
other kinds of pointers that hold extra data (for example, one of my aims
is to have 64bit pointers on a 32bit machine, and ignore the highmost
32bits of the address, while still keeping it in structures and function
parameters, so I can start to migrate my 32bit OS to 64bit, although I
don't have a 64bit CPU, and make it behave like a 64bit system where only
the first 4GB space is really used (the highmost 32 bits are allocated but
simply always ignored)).
While I am fluent with various assembly languages, I'm not so much with C++,
but I'd like to learn. So far my efforts brought me to:
template<typename Type> struct Pt {
private:
void* Address; // on my machine, a void* is 32 bit
//void* Unused; // later I will add this member, to double the sizeof(Pt)
public:
// Default Constructor
Pt() {
}
// Copy Constructor
Pt(const Pt& Object) {
Address=Object.Address;
}
Pt& operator = (const Type* Address) {
this->Address=(void*)Address;
return(*this);
}
operator Type* () {
return((Type*)Address);
}
Type& operator * () const {
return(*(Type*)Address);
}
Type& operator [] (const int Subscript) const {
return(((Type*)Address)[Subscript]);
}
Type* operator -> () const {
return(Address);
}
Pt& operator + (const int Offset) {
*((Type*)Address)+=Offset;
return(*this);
}
Pt& operator - (const int Offset) {
*((Type*)Address)-=Offset;
return(*this);
}
// No need for a Destructor
//~Pt() {
//}
};
for each type, so far I typedef'ed the corresponding pointer this way:
typedef MyType* MyTypePt;
but from now on I will do it this other way instead:
typedef Pt<MyType> MyTypePt;
and expect identical functionality, but with twice as big pointers (when
I will uncomment that "Unused" member, of course).
Will this work ALWAYS? Have I missed the overloading of some important
operator? So far things seem to work, but I'm not sure that some subtle
bugs haven't appeared somewhere.
Could anybody, please, be so kind to fix eventual errors and address me
to the eventual extensions I need to make, in order to complete my pointer
emulation via template?
Thanks!
Mario
I would like to write a pointer to type template that behaves exactly and
completely like normal pointers, which I will use as starting point for
other kinds of pointers that hold extra data (for example, one of my aims
is to have 64bit pointers on a 32bit machine, and ignore the highmost
32bits of the address, while still keeping it in structures and function
parameters, so I can start to migrate my 32bit OS to 64bit, although I
don't have a 64bit CPU, and make it behave like a 64bit system where only
the first 4GB space is really used (the highmost 32 bits are allocated but
simply always ignored)).
While I am fluent with various assembly languages, I'm not so much with C++,
but I'd like to learn. So far my efforts brought me to:
template<typename Type> struct Pt {
private:
void* Address; // on my machine, a void* is 32 bit
//void* Unused; // later I will add this member, to double the sizeof(Pt)
public:
// Default Constructor
Pt() {
}
// Copy Constructor
Pt(const Pt& Object) {
Address=Object.Address;
}
Pt& operator = (const Type* Address) {
this->Address=(void*)Address;
return(*this);
}
operator Type* () {
return((Type*)Address);
}
Type& operator * () const {
return(*(Type*)Address);
}
Type& operator [] (const int Subscript) const {
return(((Type*)Address)[Subscript]);
}
Type* operator -> () const {
return(Address);
}
Pt& operator + (const int Offset) {
*((Type*)Address)+=Offset;
return(*this);
}
Pt& operator - (const int Offset) {
*((Type*)Address)-=Offset;
return(*this);
}
// No need for a Destructor
//~Pt() {
//}
};
for each type, so far I typedef'ed the corresponding pointer this way:
typedef MyType* MyTypePt;
but from now on I will do it this other way instead:
typedef Pt<MyType> MyTypePt;
and expect identical functionality, but with twice as big pointers (when
I will uncomment that "Unused" member, of course).
Will this work ALWAYS? Have I missed the overloading of some important
operator? So far things seem to work, but I'm not sure that some subtle
bugs haven't appeared somewhere.
Could anybody, please, be so kind to fix eventual errors and address me
to the eventual extensions I need to make, in order to complete my pointer
emulation via template?
Thanks!
Mario