address of an object in C++
Is there a portable way to find the address of an instance of a class where operator& has been overloaded in C++?
Maybe you should think about some member function which returns 'this' pointer. Something like :
CFoo* CFoo::GetPtr () { return this; }
See ya'
CFoo* CFoo::GetPtr () { return this; }
See ya'
Quote:Original post by NitageI can only think of hacks (casting the instance into a reference of some basic type, or a union between pointers and references)..
I was looking for a generic way to use in template functions.
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.
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.
Thats a pretty good way to think about it. Thanks.
Quote:Original post by NitageYes, he probably wanted it to look like the object had a different address (making the operator is private is a special case).
You're saying that if any class overloaded & then the writer probably didn't want anyone getting at the objects's address anyway?
I'd still like to know if there's a portable way of doing it.
Quote:Original post by doynaxQuote:Original post by NitageYes, he probably wanted it to look like the object had a different address (making the operator is private is a special case).
You're saying that if any class overloaded & then the writer probably didn't want anyone getting at the objects's address anyway?
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".
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 :}
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement