467,910 Members | 1,772 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,910 developers. It's quick & easy.

Completely emulate a pointer using templates


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 Typestruct 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<MyTypeMyTypePt;

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

Oct 18 '06 #1
  • viewed: 1796
Share:
3 Replies
ma*********@REMOVETHISTOMAILMENOT.com wrote:
>
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 Typestruct Pt {
private:
void* Address; // on my machine, a void* is 32 bit
Why not Type* ?
//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;
What is Object.Address? Did you mean &Object or boost::addressof( Object ) ?
}
Pt& operator = (const Type* Address) {
Why the const? Do you define only pointers to const objects? Did you mean:

Type* const Address // const pointer to Type

as opposed to:

const Type* // pointer to const Type
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<MyTypeMyTypePt;

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?
One omission: you should provide comparison operators ==, !=, <, >, <=, >=.
If you want to make those emulate pointer behavior, then you should also
specialize std::less< Pt<T to get a total ordering for the use with
standard containers.

Another omission is exemplified by this piece of code:

struct X {};
struct Y : public X {};

int main ( void ) {
Pt<Yy_ptr ;
Pt<Xx_ptr ( y_ptr );
Y* y_raw;
X* x_raw ( y_raw );
}
Best

Kai-Uwe Bux
Oct 18 '06 #2
On 18 Oct 2006 07:54:17 GMT, mario.rossi wrote:
>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
In short, you cannot completely emulate a pointer. The relationship
between the type of the pointer and the type of the pointed-to object
is defined by the C and the C++ language for real pointes. It is
undefined for 'smart pointers'. You can emulate parts of the pointer
semantics if that's really what you want.

Best regards,
Roland Pibinger
Oct 18 '06 #3
On 18 Oct 2006 07:54:17 GMT in comp.lang.c++,
ma*********@REMOVETHISTOMAILMENOT.com wrote,
Pt& operator + (const int Offset) {
*((Type*)Address)+=Offset;
return(*this);
}
Pt& operator - (const int Offset) {
*((Type*)Address)-=Offset;
return(*this);
}
That part is wrong, operator+ should not modify the object.

Typically you want to implement operator+= and operator-= as member
functions, and then write operator+ and operator- as NON-member
functions using the former. That way e.g. (1+pt) works.

Oct 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gert Van den Eynde | last post: by
4 posts views Thread by firegun9 | last post: by
7 posts views Thread by Ioannis Vranos | last post: by
9 posts views Thread by shaun | last post: by
33 posts views Thread by Ney André de Mello Zunino | last post: by
2 posts views Thread by Andre-John Mas | last post: by
reply views Thread by Sin Jeong-hun | last post: by
14 posts views Thread by Eugeny Myunster | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.