|We hope you enjoy your visit.|
You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.
Join our community!
If you are already a member please log in to your account to access all of our features:
|some sort of global variable in c++; need shared access through subobjects|
|Topic Started: Jul 4 2012, 01:59 PM (149 Views)|
|Bazza||Jul 4 2012, 01:59 PM Post #1|
so here is my hierarchy
with this i have found 2 cases of necessary connectivity
probably more cases i just haven't found them all yet
was thinking of doing a static const pointer to class A or some kind of define but i don't know if/how the scope would do what i want
without including object A's header in every subobjects header file which i dont think is a good idea
basicaly i'm just looking for the simplest way to get cross object data sharing
any help anyone can give is much apreciated
Edited by Bazza, Jul 4 2012, 02:06 PM.
|My instinct is to hide in this barrel, like the wily fish.|
|Dr. Best||Jul 4 2012, 03:07 PM Post #2|
You may be interested in using forward declarations. This way you can use pointers and references to an object in the header file without including the header, which defines the object. Then in the source file you can include the header to actually do something with the object pointers. Here is a little example of this where two objects use eachother:
This example does not do anything meaningful at all. It is just meant to demonstrate that it is possible that CFoo invokes methods of CBar and CBar invokes method of CFoo.
Another thing that you might find interesting is the #pragma once keyword. It ensures, that a header is never included more than once. You simply put it at the beginning of the header. For example this code would not compile:
The problem is that CA.h is included into CC.cpp twice and thus class CA is defined twice. This will cause compiler errors. One way to escape this problem is to remove the include of CA.h from CC.h, because due to the include of CA.h in CB.h it is not needed. However this would lead to problems as soon as the include of CA.h is removed from CB.h. A cleaner solution is the use of #pragma once.
This little change makes things work. I have made used to placing #pragma once at the beginning of every header I write. It makes things a lot easier.
You may also be interested in singletons (search the web for it), but this is a solution, which is problematic in many cases. C++ also supports global variables, but I would not recommend using them for anything other than constants (like pi). These solutions share a problem with static variables. They can cause problems, if you use multi threading.
In your specific case the include hierarchy I sketched above would work well (using #pragma once). Still you may consider using forward declarations. The more includes you have in your headers, the longer will the compilation of your problem take. Thus it is always advantageous to use forward declarations to have most includes in the source files.
|« Previous Topic · Basics · Next Topic »|