address of an object in C++

Started by
11 comments, last by mfawcett 18 years, 9 months ago
Is there a portable way to find the address of an instance of a class where operator& has been overloaded in C++?
Advertisement
Maybe you should think about some member function which returns 'this' pointer. Something like :

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

See ya'
___Quote:Know where basis goes, know where rest goes.
I was looking for a generic way to use in template functions.
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.
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.

It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
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.
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.
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".
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
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 :}

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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement