Solved

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

Posted on 2000-04-18
9
208 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:dwagner
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2727053
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
 

Author Comment

by:dwagner
ID: 2727114
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
 
LVL 2

Accepted Solution

by:
fremsley earned 100 total points
ID: 2727251
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
 
LVL 5

Expert Comment

by:proskig
ID: 2727254
I hope you can use ::operator&()
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:KangaRoo
ID: 2727836
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
 
LVL 22

Expert Comment

by:nietod
ID: 2728003
>> 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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2728078
True.
0
 

Author Comment

by:dwagner
ID: 2728498
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
 

Author Comment

by:dwagner
ID: 2832599
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now