• Advertisement
Sign in to follow this  

address of an object in C++

This topic is 4606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a portable way to find the address of an instance of a class where operator& has been overloaded in C++?

Share this post


Link to post
Share on other sites
Advertisement
Maybe you should think about some member function which returns 'this' pointer. Something like :

CFoo* CFoo::GetPtr () { return this; }

See ya'

Share this post


Link to post
Share on other sites
I was looking for a generic way to use in template functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
I was looking for a generic way to use in template functions.
I can only think of hacks (casting the instance into a reference of some basic type, or a union between pointers and references)..
I'm not so sure it's a good idea to do this generically though, unless perhaps if you need to write some low-level 'hack' yourself.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
I was looking for a generic way to use in template functions.


Overloading operator& is pretty uncommon - if you've done this, ask yourself again why exactly you did it and if you really need it (can you use a normal method or function, or another operator?).
In fact, the only good reason I can see for operator& overloading is the undeletable-pointer smart pointer paradigm - and in that case, I don't see why you would be taking adresses anyway.

Share this post


Link to post
Share on other sites
You're saying that if any class overloaded & then the writer probably didn't want anyone getting at the objects's address anyway?

Thats a pretty good way to think about it. Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
You're saying that if any class overloaded & then the writer probably didn't want anyone getting at the objects's address anyway?
Yes, he probably wanted it to look like the object had a different address (making the operator is private is a special case).

I'd still like to know if there's a portable way of doing it.

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
Quote:
Original post by Nitage
You're saying that if any class overloaded & then the writer probably didn't want anyone getting at the objects's address anyway?
Yes, he probably wanted it to look like the object had a different address (making the operator is private is a special case).

I'd still like to know if there's a portable way of doing it.



Linky to an article on overloading operator&


In particular:
"overloading unary operator& for a type pretty much makes generic programming impossible for that type, because the address of an object is too fundamental a property to play with naively".

Share this post


Link to post
Share on other sites
Hello Nitage,

I belive you can always get the reg(global) & operator by scoping it.
obj* prt = ::& A; uses the global & not the classes.

by putting :: before it you use the global & operator not the classes overloaded one.

much like if you have a new overloaded class function you still want to use reg global new, you scope it ::new.

Lord Bart :}

Share this post


Link to post
Share on other sites
Quote:
Original post by Lord Bart
I belive you can always get the reg(global) & operator by scoping it.
obj* prt = ::& A; uses the global & not the classes.


Incorrect.

Share this post


Link to post
Share on other sites
your milage may vary:

template <typename T>
inline T* WhereAmI(T &t){ return reinterpret_cast<T*>(&reinterpret_cast<char&>(t));}

Share this post


Link to post
Share on other sites
You can also use (or look at the implementation of) boost::address_of which does exactly what you are looking for.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement