Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Static class problem

Static class problem

Scheduled Pinned Locked Moved C / C++ / MFC
help
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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:

    J M 2 Replies Last reply
    0
    • L Lost User

      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:

      J Offline
      J Offline
      Jason Douglas
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • L Lost User

        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:

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups