Help! in C++ How to implement 2 classes share one class
-
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.
-
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.
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
-
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
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.
-
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.
Who is designing the 3 classes? you or someone else?
-prakash
-
Who is designing the 3 classes? you or someone else?
-prakash
-
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
-
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
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. :) -
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. :)yes i agree.
-prakash
-
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.
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.