Archived

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

Is this data structure ok?

This topic is 5275 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

Hi, im working whit sort of big class wich holds some other classes inside it, like this:
class A
{
 enum
 {
  DATA1,
  DATA2;
 }
 int counter;
 // ...

 class B
 {
  // ...

 };
};
This worked fine until I needed to have access from class B to A. So what do you think this as overall and could B bossibly have access to A''s variables and enums and still be like it is now?

Share this post


Link to post
Share on other sites
I don´t know any good way to solve this although I´ve also had this problem myself. Maybe you should think your design again or if you want/have to keep it like that then you could code something like..

class B;
class A
{
A() {mB.Init(this);}

B mB;
};
class B
{
public:
void Init(A* p) {mA = p;}
protected:
A* mA;
};

well, B isn´t defined inside the A´s scope, but..

[edited by - bop_z on July 5, 2003 7:05:32 PM]

Share this post


Link to post
Share on other sites
Im not exactly sure what you are tryign to achive overall, however you could always just pass a pointer into the class B function of only the data you need to use (bop_z suggested passing a pointer to the entire class, but you wouldnt have access to the private data) you need to call... like if class B wanted to initalize a cKeyboardDevice that was located in class A you could make a funtion like...

void B::Init(cKeyboardDevice* keyboard)
{
keyboard->Initalize(blah,blah,blah);
}

and call it from inside A passing its private data in.

also for your enums, i would just make it in the global scope of your .h file for the class. That way if you wanted to use the enum (along with the class that uses it) you just include the .h

[edited by - IllMind on July 5, 2003 7:21:21 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by bop_z
I don´t know any good way to solve this although I´ve also had this problem myself. Maybe you should think your design again or if you want/have to keep it like that then you could code something like..



This is the best advice I saw posted. Perhaps your design is a little off or something. Any time I think of a plan which would rely on another class, and vice versa, I rethink it. Thinking of these things (classes) as objects, I can't really see a time you'd need mutual reliance with them.

[edited by - Peon on July 5, 2003 7:33:42 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Im not quite sure if I understand what your asking, but this sort of looks like a job for inheritance.

Share this post


Link to post
Share on other sites
I can''t remember this for sure, but I believe you can just use the scope resolution operator to access the enum.

For example:

class B {
// blah

DoSomething(A::DATA1);
// blah

}


Hmm.... I just noticed that B is inside A; I''ll have to reevaluate your question.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites
Unfortunetly the answer hasn''t found yet.

Why do I want class B to be inside class A is becose A is considered as some sort of ''helper class'' and B is only needed in A, so it would be good if it would be ''hidden''. And that is becose I don''t like having useless classes avaible there we''re they ain''t needed.

Share this post


Link to post
Share on other sites
It sounds like you really need to rethink your design. Are you saying that you want ‘B’ to be contained in ‘A’ but that ‘A’ is a helper class for ‘B’? I’m not to sure what exactly you want to do, maybe you should read up on inheritance and composition to see if that can help.

From what I understand you are trying to do, I would first of all use separate .h and .cpp files for both classes. I think it would make more sense if class ‘B’ was a ‘helper’ class for ‘A’ seeing as it is contained in ‘A’. In which case it does not need to know anything about ‘A’s private of public data you can just pass those things into ‘B’ via it’s member functions.

Share this post


Link to post
Share on other sites
I see what you''re trying to do. However, C++ does not have inner classes in the sense you''re looking for. Java has inner classes that are actually associated with the enclosing class; in C++, placing one class inside of another merely places the inner class in the enclosing class''s scope. The inner class has no special access to the enclosing one, and unlike in Java, an instance of the inner class is not associated in any way with some instance of the enclosing class from which it is instantiated.

The best you can do to emulate this Java-style scenario is to give the inner class friend access to the enclosing one, give it a pointer to an object of the enclosing class, and pass it a pointer to an "owner" object in the constructor.

Share this post


Link to post
Share on other sites
quote:
Original post by KiwiMelon
Are you saying that you want ‘B’ to be contained in ‘A’ but that ‘A’ is a helper class for ‘B’?



Oh no, I ment than A is important class, it's actualy a scripting engine class and B is just variable class what A uses to compile (but B need's thing or two from A also).

So in other words B is ONLY needed in A.

So butting B to global space would't be a bad thing but it doesn't have to be so...



[edited by - Craazer on July 7, 2003 9:32:26 AM]

Share this post


Link to post
Share on other sites
The C++ language doesn''t give any special access privileges to nested classes. Even though class B is defined within class A, B cannot access A''s private or protected members.

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
The C++ language doesn''t give any special access privileges to nested classes. Even though class B is defined within class A, B cannot access A''s private or protected members.


Ok then.

Thanks for all replys!

Share this post


Link to post
Share on other sites