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. Help! in C++ How to implement 2 classes share one class

Help! in C++ How to implement 2 classes share one class

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++helpquestion
9 Posts 4 Posters 1 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.
  • S Offline
    S Offline
    stans80
    wrote on last edited by
    #1

    now i have something as following: A a* = new A(); B b* = new B(); there is a class C created in the constructor of A, but i want the b class also can use this C class. for example: A a1* = new A(); //c class will be created (we call it c1 for differencial) A a2* = new A(); //c class will be created (we call it c2 for differencial) B b1* = new B(); // I want b1 share the c1 with a1, but not use c2 B b2* = new B(); // I want b2 share the c2 with a2, but not use c1 Is there anyway i can do this? Thanks a lot in advance.

    P 1 Reply Last reply
    0
    • S stans80

      now i have something as following: A a* = new A(); B b* = new B(); there is a class C created in the constructor of A, but i want the b class also can use this C class. for example: A a1* = new A(); //c class will be created (we call it c1 for differencial) A a2* = new A(); //c class will be created (we call it c2 for differencial) B b1* = new B(); // I want b1 share the c1 with a1, but not use c2 B b2* = new B(); // I want b2 share the c2 with a2, but not use c1 Is there anyway i can do this? Thanks a lot in advance.

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      Class A { private: C *c1; public: c1* GetThatClass() { return c1 }; } Class B { private: A *a; C *c; Public: StoreThat(Class *a1) { a = a1; } Somefunction() { c = a->GetThatClass(); c->CallMethod(); } } Something on this line.


      -prakash

      S 1 Reply Last reply
      0
      • P Prakash Nadar

        Class A { private: C *c1; public: c1* GetThatClass() { return c1 }; } Class B { private: A *a; C *c; Public: StoreThat(Class *a1) { a = a1; } Somefunction() { c = a->GetThatClass(); c->CallMethod(); } } Something on this line.


        -prakash

        S Offline
        S Offline
        stans80
        wrote on last edited by
        #3

        The code will work on i create the classes a1 and b1, but now they wont be created by me, it will be created by others, they only pass in one parameter, like A a1 = new A(1), C c1, B b1 = new B(1), then i should know c1 is shared by a1 and b1. that means b1 will go to find c1.

        P H 2 Replies Last reply
        0
        • S stans80

          The code will work on i create the classes a1 and b1, but now they wont be created by me, it will be created by others, they only pass in one parameter, like A a1 = new A(1), C c1, B b1 = new B(1), then i should know c1 is shared by a1 and b1. that means b1 will go to find c1.

          P Offline
          P Offline
          Prakash Nadar
          wrote on last edited by
          #4

          Who is designing the 3 classes? you or someone else?


          -prakash

          S 1 Reply Last reply
          0
          • P Prakash Nadar

            Who is designing the 3 classes? you or someone else?


            -prakash

            S Offline
            S Offline
            stans80
            wrote on last edited by
            #5

            ME!

            P 1 Reply Last reply
            0
            • S stans80

              ME!

              P Offline
              P Offline
              Prakash Nadar
              wrote on last edited by
              #6

              So you can very well lay down the rules how to use ur classes, you need to link all 3 classes somehow so that they know which object to use. The one i showed in the earlier post is one of the methods, there could be other method.


              -prakash

              D 1 Reply Last reply
              0
              • P Prakash Nadar

                So you can very well lay down the rules how to use ur classes, you need to link all 3 classes somehow so that they know which object to use. The one i showed in the earlier post is one of the methods, there could be other method.


                -prakash

                D Offline
                D Offline
                Dennis Gourjii
                wrote on last edited by
                #7

                The way I use is a common header. For example, you might have classes A, B and C. Then the header would be the following (assuming you have separate cpp and h files for each class): class A; class B; class C; #include "A.h" #include "B.h" #include "C.h" Then you just #include this common header in each class' header and voila! And yes - don't forget to remove other includes of A, B and C in these class headers. Works fine for me. :)

                P 1 Reply Last reply
                0
                • D Dennis Gourjii

                  The way I use is a common header. For example, you might have classes A, B and C. Then the header would be the following (assuming you have separate cpp and h files for each class): class A; class B; class C; #include "A.h" #include "B.h" #include "C.h" Then you just #include this common header in each class' header and voila! And yes - don't forget to remove other includes of A, B and C in these class headers. Works fine for me. :)

                  P Offline
                  P Offline
                  Prakash Nadar
                  wrote on last edited by
                  #8

                  yes i agree.


                  -prakash

                  1 Reply Last reply
                  0
                  • S stans80

                    The code will work on i create the classes a1 and b1, but now they wont be created by me, it will be created by others, they only pass in one parameter, like A a1 = new A(1), C c1, B b1 = new B(1), then i should know c1 is shared by a1 and b1. that means b1 will go to find c1.

                    H Offline
                    H Offline
                    Henry miller
                    wrote on last edited by
                    #9

                    Sounds like you might need a factory. class factory { factory(int num, a *A, b *B) { A =new a(num); B = new b(num, a,C); } }; class a { private: // THIS IS IMPORTANT! a(...) friend class factory class C; ... } class b { private: // AGAIN, IMPORTANT b(...) ... } Note that I skiped some details, that you will need to take care of, but this should give the idea. It occurs to me that you could do this differently with a static vector. I don't use the STL (I have to support a compiler that doesn't support modern C++), but something like this instead: class Cfactory { private: static STL::vector Cs; // I'm not sure about this syntax! The static is critical to this though static getSharedC(num) { c *C; if((C = Cs[num]) == NULL) { C = new c(num); Cs.add(num,C); } return C; } }; Again, I left out a lot of details, and I'm not even sure how the vector class works, but you should be able to make it work. The last has two major problems! The first is easy to work around, but the second could be a show-stopper. First, you need something to prevent memory leaks. Reference counting is easiest (that I know of, maybe a smart pointer would work?), something needs to make sure that c1 goes away only after both a1 and b1 is deleted. Second, this gives you global context for all c1. You can't have different two parts of the code creating their own a1, because even though the a1 class is different, those two instance share the same c1! You need to figure out how to deal with this. I can't think of anything that I'd really trust.

                    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