0
int main()
{
    GameObject* go = new GameObject();
    go->transform->localScale = Vector3(1, 1, 1);
}

i wonder if im doing this in a neat way, that is, will the second row first create a Vector3 on stack memory and then copy the object to localScale(which means two object created)? also, is it meaning that localScale is indicate to a reference in a stack memory if i do it in a local function?

liiight
  • 25
  • 1
  • 7
  • Related reading: [What are C++ temporaries?](https://stackoverflow.com/questions/15130338/what-are-c-temporaries) – user4581301 Jun 28 '20 at 16:10
  • Why are you using dynamic memory allocation at all? Why not simply `GameObject go;`? And if you absolutely *must* dynamically allocate, then why not use a `std::unique_ptr` so the object will be automatically deallocated `auto go = std::make_unique();` ? Don't use old-school `new` and `delete` unless you have a *very* good reason while writing something like a container or similar and know what you are doing. – Jesper Juhl Jun 28 '20 at 16:11
  • In order to answer all of the question, we need to know exactly what `localScale` is. If it's a `Vector3& localScale;`, this code would be fatal and rejected by the compiler. `const Vector3& localScale;`, it's legal (but unchangeable). If `Vector3 localScale;`, you get a copy. If it's something surprising, your guess is better than mine because you have more information than I do. – user4581301 Jun 28 '20 at 16:16
  • @JesperJuhl its normally not created on `main()` but on some member functions and i just need dynamic memory allocation for manage all game engine instances – liiight Jun 28 '20 at 16:19
  • @user4581301 thanks its a `Vector3 localScale` and i'll try the others – liiight Jun 28 '20 at 16:24
  • Don't try the others. Take the copy and run. The compiler will likely optimize away all of the copying fluff if it can. That's its job: Worry about the nity gritty details so the programmer can focus on describing the program behaviour they want. What you have is simple, safe, and readable and should only be changed if testing finds that you need more speed and this is the best place to get it--something I hightly doubt. – user4581301 Jun 28 '20 at 16:27

0 Answers0