Guest User

Untitled

a guest
Sep 14th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. Class A initialisation within class B containing pointer to class A
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. class A {
  6. public:
  7. A(const int pInt) {mInt = pInt;}
  8. void print() {std::cout << mInt << std::endl;}
  9. private:
  10. int mInt; //some data
  11. };
  12.  
  13. class B {
  14. public:
  15. B() {mP1 = new A(1);} //initialise to 1
  16. ~B() {delete mP1;}
  17. A& access() {return *mP1;} //return reference to the data
  18.  
  19. private:
  20. A* mP1; //pointer to some data
  21. };
  22.  
  23. int main() {
  24. B vB;
  25. vB.access().print(); //this works.
  26.  
  27. B *vBptr;
  28. vBptr->access().print(); //Segmentation fault!
  29.  
  30. std::vector<B*> vVec;
  31. vVec.resize(1);
  32. vVec[0]->access().print(); //Segmentation fault!
  33. }
  34.  
  35. B *vBptr;
  36.  
  37. B *vBptr = new B();
  38.  
  39. vVec.resize(1);
  40.  
  41. vVec.resize(1,new B());
  42.  
  43. B *vBptr = new B;
  44. vBptr->access().print();
  45.  
  46. for ( int i = 0 ; i < vVec.size() ; i++ )
  47. vVec[i] = new B;
  48.  
  49. delete vBptr;
  50.  
  51. for ( int i = 0 ; i < vVec.size() ; i++ )
  52. delete vVec[i];
  53.  
  54. B *vBptr; // bad -- uninitialized
  55. B *vBptr = new B; // proper
  56.  
  57. B vB;
  58. vB.access().print(); //this works.
  59.  
  60. B *vBptr = new B;//allocate memory for vBptr
  61. vBptr->access().print();
  62. delete vBptr;//clean up of vBptr
  63. std::vector<B*> vVec;
  64. vVec.push_back(new B);
  65. vVec[0]->access().print();
  66. delete vVec[0];
  67.  
  68. class B {
  69. public:
  70. B():mP1(new A(1)) {} //initialise to 1
  71. ~B() {} //no extra managing necessary
  72. A& access() {return *mP1;} //return reference to the data
  73.  
  74. private:
  75. std::shared_ptr<A> mP1; //pointer to some data
  76. };
  77.  
  78. int main() {
  79. B vB;
  80. vB.access().print(); //this works.
  81.  
  82. std::shared_ptr<B> vBptr(new B);
  83. vBptr->access().print();
  84.  
  85. std::vector<std::shared_ptr<B> > vVec;
  86. vVec.push_back(std::shared_ptr<B>(new B));
  87. vVec[0]->access().print();
  88. }
  89.  
  90. B *vBptr;
  91. vBptr->access().print(); //Segmentation fault!
  92.  
  93. std::vector<B*> vVec;
  94. vVec.resize(1);
  95. vVec[0]->access().print(); //Segmentation fault!
  96.  
  97. B vB;
  98. vB.access().print();
  99.  
  100. B *vBptr = &vB; // or B *vBptr = new B;
  101. vBptr->access().print();
  102.  
  103. std::vector<B*> vVec;
  104. vVec.push_back(&vB); // vVec.push_back(new B);
  105. vVec[0]->access().print();
  106.  
  107. class B {
  108. public:
  109. // B() {}
  110. // ~B() {}
  111. A& access() {return a;}
  112.  
  113. private:
  114. A a;};
  115.  
  116. #include <iostream>
  117. #include <vector>
  118. #include "boost/smart_ptr.hpp"
  119.  
  120. class A {
  121. public:
  122. A(const int pInt) {mInt = pInt;}
  123. void print() const {std::cout << mInt << std::endl;}
  124. void set(const int pInt) {mInt = pInt;}
  125. private:
  126. int mInt; //some data
  127. };
  128.  
  129. class B {
  130. public:
  131. B() {} //leave A pointer as null
  132. ~B() { } //delete handled by shared_ptr
  133. A& access() {return *mP1;} //return reference to the data
  134. boost::shared_ptr<A>& access_A_ptr() {return mP1;} //return the pointer for assignment
  135.  
  136. private:
  137. boost::shared_ptr<A> mP1;
  138. };
  139.  
  140. int main() {
  141.  
  142. std::vector< boost::shared_ptr<A> > vVecA; //data to be shared
  143. for (unsigned int i = 0; i < 5; i++) {
  144. boost::shared_ptr<A> vAptr(new A(i));
  145. vVecA.push_back(vAptr);
  146. vVecA[i]->print();
  147. }
  148.  
  149. vVecA[2]->set(123);
  150.  
  151. for (unsigned int i = 0; i < vVecA.size(); i++) {
  152. vVecA[i]->print(); //changes to the underlying objects are reflected
  153. }
  154.  
  155. boost::shared_ptr<B> vBptr(new B); //make an empty B
  156. vBptr->access_A_ptr() = vVecA[2]; //assignment of shared pointer
  157.  
  158. vBptr->access().print();
  159.  
  160. std::cout << "use count of vVecA[1] = " << vVecA[1].use_count() << std::endl; // = 1
  161. std::cout << "use count of vVecA[2] = " << vVecA[2].use_count() << std::endl; // = 2
  162.  
  163. std::vector< boost::shared_ptr<B> > vVecB;
  164.  
  165. //vVecB.resize(vVecA.size(), boost::shared_ptr<B> (new B) ); //resize and init to a SINGLE B object, NO!!!
  166.  
  167. for (unsigned int i = 0; i < vVecA.size(); i++) {
  168. vVecB.push_back(boost::shared_ptr<B> (new B)); //filling array with separate empty Bs
  169. vVecB[i]->access_A_ptr() = vVecA[i];
  170. vVecB[i]->access().print(); // = 0,1,123,3,4
  171. }
  172.  
  173. vVecA[2]->set(2); //changes to A objects reflected in the B objects
  174.  
  175. for (unsigned int i = 0; i < vVecB.size(); i++) {
  176. vVecB[i]->access().print(); // = 0,1,2,3,4
  177. }
  178.  
  179. std::cout << "use count of vVecA[1] = " << vVecA[1].use_count() << std::endl; // = 2
  180. std::cout << "use count of vVecA[2] = " << vVecA[2].use_count() << std::endl; // = 3
  181.  
  182. }
Add Comment
Please, Sign In to add comment