Solved

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

Posted on 2000-04-18
9
207 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

20 Experts available now in Live!

Get 1:1 Help Now