Sign in to follow this  
Nitage

address of an object in C++

Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this