Guest User

Untitled

a guest
Feb 19th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. //daughterstruct.hpp
  2. struct Daughter
  3. {
  4.     int** p_pa;
  5.     int** p_pb;
  6.  
  7.     Daughter()
  8.     {
  9.     }
  10.  
  11.     Daughter(int** a, int** b)
  12.     {
  13.         p_pa = a;
  14.         p_pb = b;
  15.     }
  16. };
  17.  
  18. //motherstruct.hpp
  19. #include"daughterstruct.hpp"
  20.  
  21. struct Mother
  22. {
  23.     int* p_a;
  24.     int* p_b;
  25.  
  26.     int num1;
  27.     int num2;
  28.     int num3;
  29.     int num4;
  30.  
  31.     Daughter daughter;
  32.  
  33.     Mother()
  34.     {
  35.         daughter = Daughter(&p_a, &p_b);
  36.  
  37.         p_a = &num1;
  38.         p_b = &num2;
  39.  
  40.         num1 = 22;
  41.         num2 = 0;
  42.         num3 = 65;
  43.         num4= 100;
  44.     }
  45.  
  46.     ~Mother()
  47.     {
  48.         p_a = NULL;
  49.         p_b = NULL;
  50.     }
  51.  
  52.     void swap()
  53.     {
  54.         p_a = &num3;
  55.         p_b = &num4;
  56.     }
  57. };
  58.  
  59. //main.cpp
  60. #include<iostream>
  61. #include"motherstruct.hpp"
  62.  
  63. void main()
  64. {
  65.     Mother mom;
  66.     std::cout << mom.num1 << std::endl;
  67.     std::cout << *mom.p_a << std::endl;
  68.     std::cout << **mom.daught.p_pa << std::endl;
  69.  
  70.  
  71.     // mom.p_a is pointing to mom.num3, and   mom.daught.p_pa is pointing to mom.p_a
  72.     mom.swap();
  73.  
  74.     std::cout<<mom.num3 << std::endl;
  75.     std::cout<<*mom.p_a << std::endl;
  76.     std::cout<<**mom.daught.p_pa << std::endl;
  77.  
  78.     /*
  79.     std::cout << mom.num1 << std::endl;
  80.     std::cout << *mom.p_a << std::endl;
  81.     std::cout << *mom.daught.p_pa << std::endl;
  82.  
  83.     mom.swap();
  84.  
  85.     std::cout << mom.num1 << std::endl;
  86.     std::cout << *mom.p_a << std::endl;
  87.     std::cout << *mom.daught.p_pa << std::endl;
  88.     */
  89. }
Add Comment
Please, Sign In to add comment