Link to home
Start Free TrialLog in
Avatar of thready
thready

asked on

Pointer in one class to member in another

Hi Experts,

Let's say I have this class:
class MyClass
{
public:
      void GetPointerToMyVariableOnTheStack(void ** p);

private:
      SomeOtherClass m_OnTheStack;
};

Please don't tell me not to do this- I don't care that it's wrong- I'm actually just interested in the syntax or if it's possible to do this even.

It's super easy to implement this if my member variable is a pointer.  I don't want that at all.
Also, the signature of GetPointerToMyVariableOnTheStack can change - that's what will answer my question...  So how can this be implemented?  I tried variations of  &*  and &  and I'm wondering if this is not possible for some reason...

Thanks!
Mike
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

It's not  clear from your question what it is you want to do. Are you saying you want one class to have a member pointer that points to a member in another class? Are you saying you want one class to have a member that just points to another class?
Avatar of thready
thready

ASKER

Yep.. Very badly worded... Sorry.  That's a result of rephrasing my question a number of times to remove unnecessary details and then removing too much!  :)

Basically, I just want to get a pointer to m_OnTheStack.  I don't want m_OnTheStack to itself be a pointer (it needs to remain on the stack - not on the heap (not newed))

So how do I write GetPointerToMyVariableOnTheStack in order to do this?  (how do I implement this function so that on exit, p will point to the object so that it can use it?)

Not kosher, I know, just curious.

Thanks!
Mike
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Not kosher, I know, just curious.
Meh. The best way to learn is to try different things. The fact you know it's bad practice is good enough :)
Avatar of thready

ASKER

D-oh.  I can't believe I didn't see that on my own.  Thank you!  :-)

Not actually going to use this - I have a pointer in the other class, but actually, the "other" class is really a derived class.. Just that the base class is abstract and I wanted to get a pointer to a variable that is always in scope for functions that use base class members.....  All good! :-)
As always, you're most welcome Mike.