Classes in classes, public, private?!
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?
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.
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.
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.
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]
[edited by - civguy on January 31, 2003 2:57:28 AM]
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?
It''s difficult to discuss without a specific example. Can you show us more code?
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]
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.
"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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement