Jump to content
  • Advertisement
Sign in to follow this  
nlbs

both operator&() and operator*() is overloaded

This topic is 3618 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

If a Class overloads both operator&() and that method returns something other that returning the address of actual object (itself). also if it overloads operator*() and that also returns something else other that returning the actual Object (itself) Whats the way to get the real address of the object. If I ask myself I get the answer there is no way. Is my answer right ??

Share this post


Link to post
Share on other sites
Advertisement
You can cast an object reference to a reference to a type that has a valid unary & behavior, and take the address of that. Ex:

struct A {
A * operator &() { return 0; }
};

int main(int, char **) {
A a;
int & i = reinterpret_cast<int &>(a);
std::cout << &a << "\n";
std::cout << &i;
}

Technically, this is implementation specific behavior that may or may not actually result in the address of a. Your mileage may vary.

Share this post


Link to post
Share on other sites
Hmm It should come to my mind also
So I can use.
char* c = reinterpret_cast<char*>(a);

right ??

Share this post


Link to post
Share on other sites
Maybe you could add a function like this to the class.

A* get_real_address() const {
return this;
};


If you can't modify the class in question, try SiCrane's method.

Share this post


Link to post
Share on other sites
Quote:
If a Class overloads both operator&() and that method returns something other that returning the address of actual object (itself).
also if it overloads operator*() and that also returns something else other that returning the actual Object (itself)

If you overload the bitwise AND operator then there is no problem as it requires a rhs argument to make sense, similarly if you overload the arithmetic multiplication operator.

struct foo
{
foo operator& (foo& f){return *this;}
foo* operator& (){return this;}
};
#include <iostream>
int main()
{

foo f;
foo* fp(&f);
std::cout <<fp <<std::endl;
}



If you are not doing the above then maybe you should reconsider what your operators are doing and if overloading them is __correct__.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
No. That won't compile.

would you mind to explain why it wouldn't compile ??

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!