dwagner
asked on
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
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
ASKER
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...]
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...]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I hope you can use ::operator&()
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...
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...
>> 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.
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.
True.
ASKER
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
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
ASKER
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.
This comment was about the best method I have seen. Cheers.
Like
class X
{
X* GetAddress() { return this; };
};
Let me know if you have any questions.