1

I wanted to know if it is ok to assign a value by ref like this?

Merger A = Merger();

// if Condition is true, B references to A, otherwise a different Merger.
Merger& B = Condition ? A : Merger();
class Merger
{
   //...
   void MergeMesh(Mesh mesh);
   void CreateAsset();
   void ReleaseResources();
   //...
};

I wanted to simplify my code. Merger is a small utility to merge different 3d models together. now if a condition is true, I wanted to split the merge result into two without duplicating the code.

{
    Merger A = Merger();

    Merger& B = MergeAll ? A : Merger();

    // then through some loops and conditions i will have
    A.MergeMesh(mesh1);
    A.MergeMesh(mesh2);

    // or sometimes
    B.MergeMesh(mesh3);
}

I guess given the answers I can do something like this.

{
    Merger A = Merger();
    Merger B = Merger();

    A.Init();

    Merger& C = MergeAll ? A : B;

    if(!C.IsInit()) C.Init();
}
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • 2
    No, that's not okay, because the instance of Foo is temporary. – paddy Aug 21 '22 at 13:07
  • What if I only use the B as temporary? let me fix my question then – M.kazem Akhgary Aug 21 '22 at 13:07
  • `Foo()` creates a temporary instance of the `Foo` class. That instance will be destructed almost immediately. You can't have references to temporary instances or values. It works for `Foo A = Foo();` because here you make a *copy* of the temporary object before it ceases to exist. – Some programmer dude Aug 21 '22 at 13:09
  • So I can't simplify my code like that even if I use A and B in a single scope? – M.kazem Akhgary Aug 21 '22 at 13:10
  • 1
    B is not a temporary. B is a named reference. A temporary is an unnamed object created as a result of an expression. `Foo()` is a temporary. You cannot bind a non-const reference to a temporary. Conditionals have nothing to do with it. You can do `Foo& B = Condition ? X : Y` if both `Foo& B = X` and `Foo& B = Y` are valid. `Foo& B = Foo()` is *not* valid. – n. m. could be an AI Aug 21 '22 at 13:12
  • Change the `Foo& B =...` to `Foo const& B =...`. – Eljay Aug 21 '22 at 13:12
  • Ah, I see. Thanks for the answers. I want to operate on Foo, it's a mini utility, so i can't use const. I guess I will look for a work around then. – M.kazem Akhgary Aug 21 '22 at 13:14
  • 2
    Your question have the smell of an [XY problem](https://xyproblem.info/). What is the *actual* problem you need to solve? Why do you think the shown solution is the only solution to that (for us) unknown problem? – Some programmer dude Aug 21 '22 at 13:14
  • @Someprogrammerdude Foo is just a utility (`Mesh Merger`) with bunch of functions to merge different meshes together. for example `Merger.MergeStaticMesh(x)`. so I can call `MergeStaticMesh` multiple times and at the end I do something like `Merger.CreateAsset();` to get the final result and then `Merger.ReleaseResources();`. now If a condition is true, I want to have two sets of merged meshes, otherwise all meshes should be merged into one. I just wanted to simplify my code. – M.kazem Akhgary Aug 21 '22 at 13:21
  • 1
    When you don't understand basic object lifetime, it's best not to simplify your code examples. Just show what you have. – paddy Aug 21 '22 at 13:24
  • @Someprogrammerdude I have updated my questions, But I think I got a workaround thanks to your answers :) – M.kazem Akhgary Aug 21 '22 at 13:29

0 Answers0