?
Solved

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

Posted on 2000-04-18
9
Medium Priority
?
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:proskig
ID: 2727254
I hope you can use ::operator&()
0
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

765 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