Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A { //here is a class A
- public:
- int *a;
- A(){ a = new int; *a=22;}
- A foo(){ A anA; //anA is an object of class A
- *anA.a=44;
- return anA;
- }
- ~A(){ delete a;}
- };
- int main(){
- A B;
- B=B.foo();
- //What is the value of B.a at this line of the code
- }
- #include <cstdio>
- class A
- {
- public:
- int *a;
- A()
- {
- a = new int;
- printf("A::A(0x%p): a is 0x%pn", this, a);
- *a = 22;
- }
- A foo()
- {
- A anA;
- *anA.a = 44;
- return anA;
- }
- ~A()
- {
- printf("A::~A(0x%p): a is 0x%pn", this, a);
- delete a;
- }
- };
- int main(int argc, char** argv)
- {
- A B;
- B = B.foo();
- }
- #include <cstdio>
- class A
- {
- public:
- int *a;
- A()
- {
- a = new int;
- printf("A::A()(0x%p): a is 0x%pn", this, a);
- *a = 22;
- }
- A(const A& otherA)
- {
- a = new int;
- printf("A::A(const A& otherA)(0x%p): a is 0x%pn", this, a);
- *a = *otherA.a;
- }
- A& operator=(const A& otherA)
- {
- printf("A::operator=(const A& otherA)(0x%p)n", this);
- // What are the semantics here? Transfer ownership? Copy Value?
- *a = *otherA.a;
- return *this;
- }
- A foo()
- {
- A anA;
- *anA.a = 44;
- return anA;
- }
- ~A()
- {
- printf("A::~A(0x%p): a is 0x%pn", this, a);
- delete a;
- }
- };
- int main(int argc, char** argv)
- {
- {
- A B;
- B = B.foo();
- printf("B.a is %dn", *B.a);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement