Guest User

Untitled

a guest
Feb 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. ///daughterstruct.hpp
  2.  
  3.  
  4. struct DAUGHTER
  5. {
  6.  
  7. int** p_pa;
  8.  
  9. int** p_pb;
  10.  
  11. DAUGHTER(){}
  12.  
  13.  
  14. DAUGHTER::DAUGHTER(int** a, int** b)
  15. {
  16. p_pa=a;
  17. p_pb=b;
  18. }
  19.  
  20.  
  21. };
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. //motherstruct.hpp
  29.  
  30. #include"daughterstruct.hpp"
  31.  
  32. struct MOTHER
  33. {
  34.  
  35. int* p_a;
  36. int* p_b;
  37.  
  38. int num1;
  39. int num2;
  40. int num3;
  41. int num4;
  42.  
  43. DAUGHTER daught;
  44.  
  45. MOTHER()
  46. {
  47. daught=DAUGHTER(&p_a, &p_b);
  48.  
  49. p_a= &num1;
  50. p_b= &num2;
  51.  
  52. num1= 22, num2= 0, num3= 65, num4= 100;
  53. }
  54.  
  55. ~MOTHER()
  56. {
  57. p_a= NULL;
  58. p_b= NULL;
  59. }
  60.  
  61. void swap()
  62. {
  63. p_a= &num3; p_b= &num4;
  64. }
  65.  
  66. };
  67.  
  68.  
  69.  
  70.  
  71. //main.cpp
  72.  
  73. #include<iostream>
  74.  
  75. using namespace std;
  76. #include"motherstruct.hpp"
  77.  
  78. void main()
  79. {
  80. MOTHER mom;
  81.  
  82. //what I'm doing
  83.  
  84. cout<<mom.num1<<endl;
  85. cout<<*mom.p_a<<endl;
  86. cout<<**mom.daught.p_pa<<endl;
  87.  
  88. //swap
  89.  
  90. mom.swap(); //mom.p_a is pointing to mom.num3, and mom.daught.p_pa is pointing to mom.p_a
  91. //this would be easier if I just had a reference to mom.p_a in the daughter class!
  92.  
  93. cout<<mom.num3<<endl;
  94. cout<<*mom.p_a<<endl;
  95. cout<<**mom.daught.p_pa<<endl;
  96.  
  97. ///
  98.  
  99.  
  100.  
  101.  
  102. //what I'm tryng to do
  103. /*
  104.  
  105. cout<<mom.num1<<endl;
  106. cout<<*mom.p_a<<endl;
  107. cout<<*mom.daught.p_pa<<endl; //mom.daught.p_pa is a reference to mom.daught.p_a
  108.  
  109. //swap
  110.  
  111. mom.swap();
  112.  
  113. cout<<mom.num1<<endl;
  114. cout<<*mom.p_a<<endl;
  115. cout<<*mom.daught.p_pa<<endl;
  116.  
  117.  
  118. */
  119.  
  120.  
  121.  
  122. }
Add Comment
Please, Sign In to add comment