• Advertisement

Archived

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

Classes in classes, public, private?!

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wasn''t working a lot with classes for a long time when I began learning how to do OpenGL, but now I need them and forgot a probably simple thing: If I create a class, with another class as variable inside that one, is the private content of the second class available to the first one? Something like this: class B { private: int x; public: int y; }; class A { private: B SecClass; public: void DoSomethingToSecClass(); }; void A::DoSomethingToSecClass() { SecClass.x = 0; } Then calling A.DoSomethingToSecClass would change the private variable in that SecClass, wouldn''t it?

Share this post


Link to post
Share on other sites
Advertisement
no. If class B has a private member, no other class can access it. You either need to make it public, or make a function that returns a pointer or the variable itself.

Share this post


Link to post
Share on other sites
Ah, thanks.
I just thought that maybe A would be considered the "parent" of B or something, but it''s ok like this.
I guess I''ll just make it all public in class B, since no other part of the program will even touch it. There should be no problems.

Share this post


Link to post
Share on other sites
If only A needs to access B's private values, you can make A to be B's friend

[edited by - civguy on January 31, 2003 2:57:28 AM]

Share this post


Link to post
Share on other sites
It maybe that class B can do the ''something'' itself rather than having to expose itself. Getting/Setting isn''t really the point, and just returning a pointer to the internal data is as bad as having the data public.

It''s difficult to discuss without a specific example. Can you show us more code?

Share this post


Link to post
Share on other sites

On the other hand, this *should* work, if your compiler can take it

    
class B
{
private:
int x;
public:
int y;

class A;
};

class B::A
{
private:
B SecClass;
public:
void DoSomethingToSecClass();
};


void B::A::DoSomethingToSecClass()
{
SecClass.x = 0;
}

int main()
{
B::A foo;
foo.DoSomethingToSecClass();
}



Since the class A is declared as a member of B, it has access to B's private members


edit - fixed declaration error

edit - As Mr Jack pointed out, I'm wrong.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on January 31, 2003 3:10:44 AM]

[edited by - Fruny on January 31, 2003 5:07:00 AM]

Share this post


Link to post
Share on other sites
Fruny: No, you are mistaken...

"The members of a member class have no special access to members of an enclosing class. Similarly members of an enclosing class have no special access to members of a nested class..."

- Stroustrup, The C++ Programming Language, 3rd Edition, p.851.

Share this post


Link to post
Share on other sites

  • Advertisement