-2

kindly, some questions to answer and statements to confirm or to amend in case. I want to make sure I got it right and I am thankful for any hint. Further I could imagine that those examples as a whole have some value for C++ beginners.

  1. Stack initiated object

MyClass c(10);
MyClass c = MyClass(10);

To my understanding these two object initializations can be used interchangeably, correct ? Further they are automatically cleaned up when they go out of scope, for example returning form a function.

  1. Heap initiated object

MyClass* c = new MyClass(10);

This object needs to be cleaned up manually, like "delete c".

  1. The object bug function

MyClass* getObj() {
        MyClass c(10); // stack initiated object
        return &c;
}

This will return a pointer to my stack scoped MyClass object (as I am not using the new keyword). And as a matter of fact I should not be able to use the pointer returned to it as at the time of usage the object might be cleaned up already or fail later. Correct ?

  1. The valid object copy function

MyClass getObj() {
        MyClass c(10); // stack initiated object
        return c;
}

In this case a copy of the stack initiated object will be returned valid for the scope of the caller, correct ?

Thank you in advance.

Kind regards, Hermann

trincot
  • 317,000
  • 35
  • 244
  • 286
user2050516
  • 760
  • 2
  • 5
  • 15
  • 1
    `MyClass c = new MyClass(10);` needs to be `MyClass* c = new MyClass(10);` – juanchopanza Sep 18 '14 at 08:43
  • Get a good book and read it: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – rubenvb Sep 18 '14 at 08:59
  • All of these questions have already answers: [here](http://stackoverflow.com/questions/3735839/c-stack-variables-and-heap-variables), [here](http://stackoverflow.com/questions/4570366/pointer-to-local-variable), or [here](http://stackoverflow.com/questions/3350385/how-to-return-an-object-in-c) – MatthiasB Sep 18 '14 at 09:01

1 Answers1

2

1°) Yes, though if you want to be safe, you should not use the syntax MyClass a(10); as you may encounter most vexing parse problems. Ideally, you could use the following syntax (assuming c++11) which express initialization without any ambiguity:

MyClass a{10};

2°) Yes. But you should rely on RAII and use smart pointers whenever you can and want to allocate resources.

3°) This is bad, as the pointer you returns points to memory that has been released automatically when leaving the function (when the local object c has been destroyed).

4°) Yes, but even better, with named value return optimization, you should not even end up with a copy, as the compiler is usually clever enough to see there's no point in destroying the local object and returning a copy.

JBL
  • 12,588
  • 4
  • 53
  • 84
  • Note that in **1)** `T t = T(args);` requires that the type be copyable or move copyable. – juanchopanza Sep 18 '14 at 09:09
  • @juanchopanza Oh? I thought I saw somewhere that compilers were usually able to reduce this expression to a simple ctor call _when the types are exactly the same_, so as long as there's no conversion implied. – JBL Sep 18 '14 at 09:12
  • The compiler can elide the copy, otherwise it can do whatever it wants as long as it follows the "as-if" rule, but the code still needs to be valid, and a copy or move-copy is required, even if no copy is actually made. See http://ideone.com/KFxbaA – juanchopanza Sep 18 '14 at 09:19