Static class problem
-
I have a very annoying problem. I have two static classes, declared right after each other and somehow, I want them to be able to reach each other... static class A : public Location { public: Location *GetB { return &B; } } A; static class B : public Location { public: Location *GetA { return &A; } } B; The code above is how I 'want' it to work. Any help on this would be very appreciated. Erik :confused:
-
I have a very annoying problem. I have two static classes, declared right after each other and somehow, I want them to be able to reach each other... static class A : public Location { public: Location *GetB { return &B; } } A; static class B : public Location { public: Location *GetA { return &A; } } B; The code above is how I 'want' it to work. Any help on this would be very appreciated. Erik :confused:
First of all, let me say that I've never seen ANYONE declare a static class in the same manner as a struct typedef. I would recommend changing your code for clarity's sake, but that's now back to your question... If you would like each class to access the other, I would recommend the following:
class A : public Location { public: static A instance; Location* GetB() { return &B::instance; }; } class B : public Location { public: static B instance; Location* GetA() { return &A::instance; }; }
Hope this helps! (Basically, you create two singleton objects.) ;) -
I have a very annoying problem. I have two static classes, declared right after each other and somehow, I want them to be able to reach each other... static class A : public Location { public: Location *GetB { return &B; } } A; static class B : public Location { public: Location *GetA { return &A; } } B; The code above is how I 'want' it to work. Any help on this would be very appreciated. Erik :confused:
You need to forward-declare class B before the definition of class A. (And BTW, your naming is confusing - a class "A" and a variable "A" together is a no-no if you care about readability.)
class B; // forward declaration
class A : public Location
{ ... } objectA;class B: public Location
{ ... } objectB;--Mike-- http://home.inreach.com/mdunn/ Time is an illusion; lunchtime doubly so.