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
LVL 1
threadyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
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?
threadyAuthor Commented:
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
evilrixSenior Software Engineer (Avast)Commented:
Something like this...

class SomeOtherClass {};

class MyClass
{
public:
      void GetPointerToMyVariableOnTheStack(SomeOtherClass ** pp) const
      {
      	*pp = &m_OnTheStack;
      }

private:
      SomeOtherClass m_OnTheStack;
};

int main() {
	
      SomeOtherClass * p = nullptr;
      MyClass myClass;
      myClass.GetPointerToMyVariableOnTheStack(&p);
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

evilrixSenior Software Engineer (Avast)Commented:
>> 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 :)
threadyAuthor Commented:
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! :-)
evilrixSenior Software Engineer (Avast)Commented:
As always, you're most welcome Mike.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.