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

## Recommended Posts

I spose this is a beginner's question. How would you go about getting the address of an object that has the & operator overloaded? (I don't actually need to do this, just for interest) I did something like:
Object o;
Object* ptr = (Object*) &((char&)o);

And it seemed to work. Is there a better way? Is this the worst possible way to do it?

##### Share on other sites
This question is similar to: "How can I take the address of an integer variable while the integer has the & overloaded ?"
int Num1, Num2 ;int *p;*p = Num1 & Num2 ; //Correct*p = &Num1 & &Num2 ; //Incorrectp = &Num1 & &Num2 ; //Incorrectp = &Num1 PLUS &Num2 ; //Incorrectp = Num1 & Num2 ; //Incorrect

Which operation will be performed really depends on the type of two objects being evaluated.
I guess casting is a possible workaround for this.

##### Share on other sites
I guess the best way would be to not overload the & operator. Isn't the point of overloading it to make something other then the adress be returned instead of the acctaul adress? I personally never found a use for overloading the & operator, but I guess that if you do, you wouldn't need the adress of the object anyway.

##### Share on other sites
Overloading the unary & operator is something quite rare. If you are using a class that has it that way, it means the person who write the code didn't want you to be able to get the address. For example, one situation I can think of is if you have a table or list class that containts objects, and the & operator is used to return the position of the object inside the table. The table itself can move around the objects in memory for whatever reasons, so it wants you to "reference" the objects only through their position, because the actual address you obtain at some point may become invalid later. If the & operator is overloaded for good reason, you don't need the address in the first place. If you do need it, it means overloading & was a bad choise.

##### Share on other sites
The Boost Python changelog claims that wrapping classes that have an overloaded address-of operator is supported. If you want to dig into their code to figure out how they did it, be my guest. But you're on your own.

Moved to General Programming (overloaded address-of operators are definitely not a beginner topic).

##### Share on other sites
I think the following is valid with regards to the C++ standard, but I'd have to double check to be sure. It does work under VC++ 8.0, Borland C++ 5.6.4 and MinGW 3.3.1. Cast the object to a char & and take the address of the char & (EDIT: which if I'd read what you posted more carefully I would have realised is exactly what you were doing):
#include <iostream>struct Thing{	Thing()	{		std::cout << "this: " << this << '\n';	}	Thing * operator&() const	{		return 0;	}};int main(){	Thing t;	std::cout << "op& : " << &t << '\n';	char & c = reinterpret_cast< char & >(t);	// cast to void * so cout prints as a pointer value, not a string literal	std::cout << "real: " << reinterpret_cast< void * >(&c) << '\n';}

Enigma

##### Share on other sites
As usual, Boost has the answer: addressof(). It just wraps the same technique in a nicer fashion.

##### Share on other sites

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