#object initialization

11 messages · Page 1 of 1 (latest)

ivory shoal
#

Inside the makeTest function, is the test object initialized on the stack or the heap?

public:
  int a;
  test(int a):a(a){}
};

test makeTest(){
  return test(4);
}```
atomic swiftBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

eager root
#

Stack.

ivory shoal
#
int main() {
  test t = makeTest();
  cout << t.a;
}
#

If I call this in the main method

#

it's able to retrieve t.a, but shouldnt that be deleted because it's out of the makeTest method

eager root
#

No, because makeTest() returns the test by-value.
Some copying takes place and the t variable receives that copy.

ivory shoal
#

does a function always return a copy?

#

cause I remember trying to create an array in a function but that getting deleted if it wasnt in the heap

#

oh that might be cause an array is a pointer

#

!solved