fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. class A {
  11. public:
  12.  
  13. int x, y, z;
  14.  
  15. A() {
  16. this->x = 0;
  17. this->y = 0;
  18. this->z = 0;
  19. A::GetA_vector().push_back(this);
  20. }
  21.  
  22. ~A() { }
  23.  
  24. void init(int x_in, int y_in, int z_in) {
  25. this->x = x_in;
  26. this->y = y_in;
  27. this->z = z_in;
  28. }
  29.  
  30. int sum() {
  31. return this->x + this->y + this->z;
  32. }
  33.  
  34. void show_result() {
  35. int result = sum();
  36. cout << "sum = " << result << endl;
  37. }
  38.  
  39. static vector<A*> & GetA_vector() {
  40. static vector<A*> A_vector;
  41. return A_vector;
  42. }
  43.  
  44.  
  45. };
  46. //vector<A*> A::A_vector;
  47.  
  48.  
  49. class B {
  50. public:
  51. A A_car, A_cat, A_canada;
  52.  
  53. B() {
  54. A_car.init(0, 1, 2);
  55. A_cat.init(1, 2, 3);
  56. A_canada.init(8, 7, 6);
  57. }
  58.  
  59. ~B() {}
  60.  
  61. void show_all_result() {
  62. for (int i = 0; i < A::GetA_vector().size(); i++)
  63. A::GetA_vector()[i]->show_result();
  64. }
  65. };
  66.  
  67.  
  68. int main() {
  69.  
  70. B obj_B;
  71.  
  72. obj_B.show_all_result();
  73. printf("done\n");
  74. system("pause");
  75. return 0;
  76. }
  77.  
  78.  
Success #stdin #stdout #stderr 0s 4512KB
stdin
Standard input is empty
stdout
sum = 3
sum = 6
sum = 21
done
stderr
sh: 1: pause: not found