Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. class A { //here is a class A
  2. public:
  3. int *a;
  4.  
  5. A(){ a = new int; *a=22;}
  6.  
  7. A foo(){ A anA; //anA is an object of class A
  8. *anA.a=44;
  9. return anA;
  10. }
  11.  
  12. ~A(){ delete a;}
  13.  
  14. };
  15.  
  16. int main(){
  17.  
  18. A B;
  19.  
  20. B=B.foo();
  21.  
  22. //What is the value of B.a at this line of the code
  23. }
  24.  
  25. #include <cstdio>
  26.  
  27. class A
  28. {
  29. public:
  30. int *a;
  31.  
  32. A()
  33. {
  34. a = new int;
  35. printf("A::A(0x%p): a is 0x%pn", this, a);
  36. *a = 22;
  37. }
  38.  
  39. A foo()
  40. {
  41. A anA;
  42. *anA.a = 44;
  43. return anA;
  44. }
  45.  
  46. ~A()
  47. {
  48. printf("A::~A(0x%p): a is 0x%pn", this, a);
  49. delete a;
  50. }
  51.  
  52. };
  53.  
  54. int main(int argc, char** argv)
  55. {
  56. A B;
  57. B = B.foo();
  58. }
  59.  
  60. #include <cstdio>
  61.  
  62. class A
  63. {
  64. public:
  65. int *a;
  66.  
  67. A()
  68. {
  69. a = new int;
  70. printf("A::A()(0x%p): a is 0x%pn", this, a);
  71. *a = 22;
  72. }
  73.  
  74. A(const A& otherA)
  75. {
  76. a = new int;
  77. printf("A::A(const A& otherA)(0x%p): a is 0x%pn", this, a);
  78. *a = *otherA.a;
  79. }
  80.  
  81. A& operator=(const A& otherA)
  82. {
  83. printf("A::operator=(const A& otherA)(0x%p)n", this);
  84. // What are the semantics here? Transfer ownership? Copy Value?
  85. *a = *otherA.a;
  86. return *this;
  87. }
  88. A foo()
  89. {
  90. A anA;
  91. *anA.a = 44;
  92. return anA;
  93. }
  94.  
  95. ~A()
  96. {
  97. printf("A::~A(0x%p): a is 0x%pn", this, a);
  98. delete a;
  99. }
  100.  
  101. };
  102.  
  103. int main(int argc, char** argv)
  104. {
  105. {
  106. A B;
  107. B = B.foo();
  108. printf("B.a is %dn", *B.a);
  109. }
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement