How do I get the address of an object after overloading the & operator?

Hi,

If I have a class where I overload the & operator to do something other than return a pointer to the instance of the object, how could I then actually get the address of that object in code? And can it be done without any nasty type casting (which is the only way I could think of doing it).

For the interested, I was playing with the CComPtr object in Visual C++ and I was wondering how to get the address of the CCOmPtr object rather than the interface pointer. I know there is no real reason to do that, I just wondered if and how it could be done.

bye...
Dave
dwagnerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
fremsleyConnect With a Mentor Commented:
Here is some sample code that uses typecasting, but not of the nasty sort (it's mostly safe to cast to a base class):

#include <stddef.h>

class A {
  public :
    int my_member; // paranoia?
};


class B : public A {
  public :
    B* operator&() { return NULL; };
};

#include <stdio.h>

int main() {
  B foo;
  puts((&foo == NULL) ? "B::& == NULL" : "B::& != NULL");
  puts((&(A&)foo == NULL) ? "A::& == NULL" : "A::& != NULL");
  return 0;
}
 
Hope it helps
0
 
nietodCommented:
You could write a member function that returns "this"

Like

class X
{
   X* GetAddress() { return this; };
};


Let me know if you have any questions.
0
 
dwagnerAuthor Commented:
Whoops, I should have been more specific. After reading my question, its kind of ambiguous :)

What I really want to know is, is there any way to force a operator not to be overloaded when you apply it to an object. For example, could I force the address of operator to actually be the address of operator rather than what it had been overloaded as?


bye...
Dave
[One of these days I'll learn to proof read stuff I write...]
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
proskigCommented:
I hope you can use ::operator&()
0
 
KangaRooCommented:
Mhh, nasty problem. The inheritance thing looks good, but could remotely be a problem with MI. Which you control so that isn't such a problem. In general it appears that a cast to any other type would do though.

Seems a bit strange though, first you persuade the compiler into doing something special you want it to do by overloading operator& and then you change your mind and want it to do as usual...
0
 
nietodCommented:
>> I hope you can use ::operator&()
I don't think there is a global operator & like that.

>> In general it appears that a cast to any other type would do though
Except how do you do the cast?  In general the only cast that is guaranteed to compile will be a pointer cast (or a cast to base, which was suggested).  but you can't do a pointer cast as you can't generate the pointer.   so you have to rely on the class being convertable to some other type, for that, you might as well add a "return pointer" member function.

>> Seems a bit strange though
Yes.

0
 
KangaRooCommented:
True.
0
 
dwagnerAuthor Commented:
I fully agree its an unusual thing to do, I was just wondering if it was possible. It would be better to redesign than have to hack something out (as I did).

Kind of like goto's I suppose... I know they exist, but would be very reluctant to use one. I'd sooner write documentation :)


bye...
Dave
0
 
dwagnerAuthor Commented:
Damn, I have a bad memory... I forgot to accept an answer for this question :)

This comment was about the best method I have seen. Cheers.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.