Archived

This topic is now archived and is closed to further replies.

I have a (C++ OOP) question...

This topic is 5444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello all, I have a (C++ OOP) question... I want to know how to (if it is even possible and what would be the best way) give a class (call it classB) access to another classes’ private data members (Lets call this class classA). (Pseudo) Code sniplet to illustrate:
class classB
{
classB();
virtual ~classB();

DoSomething()
{
m_pD3D->GetAdapterCount();      // <-- This is what I want to

//     acomplish; using m_pD3D as if it

//     were property of classB...

}
}

class classA
{
classA()
{
}
virtual ~classA();

private:
LPDIRECT3D8        m_pD3D;          // The main D3D object

}

I *think* what I want to do is make classB a friend of classA ... but every time I read the documentation on the friend keyword it never computes that way. Now what I want to accomplish (listed because I may be all confused and going about it all wrong) is to have an application class (derived from CD3Dapplication from the DX8 SDK common files.) and have multiple states (i.e. menu, intro, playing_game, etc.). Now as each state needs to know about certain events (FrameMove(), Render(), etc.) What I have in mind is to have each state in a class and have pointers to these classes in the application class. Then during FrameMove(), etc. the active state (which is a pointer to whichever state is active; set when the app state changes) is used to get the state to call FrameMove(), etc. (I.e. the FrameMove() of the app class might look something like this:
// CMyD3DApplication is derived from CD3DApplication (from dx8 sdk common files)

HRESULT CMyD3DApplication::FrameMove()
{

// FrameMove the active state

m_pActiveState->FrameMove();

// if the menu is active, FrameMove it.

{
}
}

Comments/Questions/Ideas/Suggestions/anything else welcome Thankie, Feral *edits: source tags, not code; no tabs. (= I need to post more I guess hehe* [edited by - FeralofFireTop on July 26, 2003 3:58:23 AM]

Share on other sites
don''t know about part 2 but as for accessing another class''s private members, thats where you have something like:

LPDIRECT3D8 classA::GetD3D() {   return m_pD3D;}

Share on other sites
There are other ways of accomplishing this.

First, the friend operator only works if you have a pointer to such a class. It has been done to access private and protected members just like you would access public members of that class. In your case, it''s not of a great use since you don''t seem to have instances of these classes available, and you''re not accessing private members anyway (I think).

Second, many of thse classes seem to have only a single instance in the whole game. That is, there will be only one StateMenu, only one Activestate, etc. This can be achieved by using static variables.

class OneInstanceOnly {static OneInstanceOnly * s_instance;public:static OneInstanceOnly * GetInstance( void ) {    return( s_instance );}static SetInstance( OneInstanceOnly * New ) {    s_instance = New;}};// you can access elements like this:OneInstanceOnly::GetInstance()->MemberElement;

This makes it easier, since anywhere in your program you can access the static functions or variables of any class, and from there access the current instance, and their members.

ToohrVyk

Share on other sites
First off, thank you both for your input

I should have mentioned I’d like to be able to access m_pD3D just as “m_pD3D” as if it were a normal data member of classB. Truthfully, this is mostly just for cosmetic reasons, I guess... mainly so I don’t get confused! Heh.

Hum, Phrased like that a #define macro comes to mind to simplify a singleton::get() methodology. Hum, that might be the best approach.

AP:
Very true indeed, thank you.

ToohrVyk:
quote:
Original post by ToohrVyk
First, the friend operator only works if you have a pointer to such a class. It has been done to access private and protected members just like you would access public members of that class. In your case, it''s not of a great use since you don''t seem to have instances of these classes available, and you''re not accessing private members anyway (I think).

Ok, from what you have mentioned the documentation made more sense this time
Lets see if I understand this right:

class classB{    classB();    virtual ~classB();    DoSomething()    {        m_pD3D->GetAdapterCount();      // <-- This is what I want to                                        //     acomplish; using m_pD3D as if it                                        //     were property of classB...    }}class classA{    friend class classB;                // ADDED: We like classB, share private                                        //        and protected members.    classA()    {    }    virtual ~classA();private:    LPDIRECT3D8        m_pD3D;          // The main D3D object}

This should give classB access to the private and protected members of classA, so classB::m_pD3D is valid and would be (in effect) a private data member. (for classB only of course, as friendship is not inherited.)

I thought I tried this once and discarded the idea for some reason... Heh, probably that friendship is not inherited part... I believe I was trying to make the abstract base class for the appstate class a friend of the application class. (i.e. classC derives from classB but does not have access to m_pD3D, et al. which was a problem.)

Ok, great! An an if that does not work in the actual mess of code for some reason I’ll go with singleton’s and Get member functions Thank you both!

quote:
Original post by ToohrVyk
Second, many of thse classes seem to have only a single instance in the whole game. That is, there will be only one StateMenu, only one Activestate, etc. This can be achieved by using static variables.

Aye, indeed. Many such classes (all the states, logger, etc.) are good singleton candidates.

Thank you both for your time, thoughts and replies!
Feral

Share on other sites
Ahh, silly me I misunderstood the friend keyword entirely. As I wanted to access m_pD3D as “m_pD3D” friend does nothing for me, as mentioned. ... I was wondering how it magically allowed me access to another classes’ things. Heh, silly me.

Looks like I’ll be using singletons and gets then. … Heh, already did that method, now where did I put that code......

Thank you both again.

Feral

• 11
• 19
• 12
• 34
• 9
• Forum Statistics

• Total Topics
631398
• Total Posts
2999838
×