Circular Dependencies
I know this isn''t exactly the place to post language specific questions, but I figure alot of you guys know your stuff, so I''d give it a shot.
I know that you can avoid circular dependencies between classes by using a forward declaration, and having at least the first class reference the other only via pointer. My problem is that I needed to access a structure defined inside a class. Since you can''t access member objects of a class before it''s read, this didn''t seem possible. I ended up just moving the necessary structure outside of the class it was in.
Is there a way to do this. It looked something like this...
[foo.h]
#include "bar.h"
class foo {
struct foo_struct { ... };
}
[bar.cpp]
#include "foo.h"
#include "bar.h"
void bar::bar_tool( foo::foo_struct& fs ){ ... }
I tried using "class foo" "struct foo::foo_struct" as forward declarations. In the end I just moved the foo_struct out and used "struct foo_struct", and everything worked, but it felt like a hack. Thanks for any help.
Ut
What are you really trying to do? (not how you''re trying to do it)
I''ve done stuff kinda like that, and it worked fine.
Magmai Kai Holmlor
- The disgruntled & disillusioned
I''ve done stuff kinda like that, and it worked fine.
Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
I believe for what you are doing you would normally define the structure outside the class since it is used outside the class. The foo::foo_struct should work if the structure is defined in the public section, but is cumbersome. You would normally only define it in the class when the usage is local to the class. The usage is not local to the class in your example becuase the bar class uses it as well. If your reason for defining it within the class is fear of name conflicts then you should look at using namespaces.
If there is a name conflict within the program then the compiler will tell you it can''t resolve foo::foo_struct and somethingelse::foo_struct. Then you have to add the foo:: or somethingelse:: to get it to compile, but otherwise it is unneeded because of the using namespace statement.
namespace foo{ struct foo_struct {...]; class foo{foo_struct foovar};}using namespace foo;
If there is a name conflict within the program then the compiler will tell you it can''t resolve foo::foo_struct and somethingelse::foo_struct. Then you have to add the foo:: or somethingelse:: to get it to compile, but otherwise it is unneeded because of the using namespace statement.
Keys to success: Ability, ambition and opportunity.
It''s not really my code, but someone else''s that I''m modifying. I guess it''s not as much of a hack to bring it out of the class as I thought. I was just trying to keep the same structure and was wondering if there was some neat trick to do it. I guess all it took was a different perspective. Thanks.
Ut
Ut
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement