Is this data structure ok?

Started by
11 comments, last by Craazer 20 years, 9 months ago
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?
Advertisement
I think doing this should solve it.
class A;class B;class A{ enum {  DATA1,  DATA2; } int counter; // ... class B {  // ... };};



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]
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]
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]
Peon
Im not quite sure if I understand what your asking, but this sort of looks like a job for inheritance.
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
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
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.

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.
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.

This topic is closed to new replies.

Advertisement