1

Hi i have this example.

class Test
{
public:
    Test(){m_x = 0;};
    ~Test() {};
    void setX(const int x) {m_x=x;}
    int getX() const {return m_x;}
private:
    int m_x;
};

void SetX(Test& test)
{
    int x = 2;
    test.setX(x);
}

int main()
{
    Test X;
    SetX(X);
    std::cout << "XX: " << X.getX() << std::endl;
return 0;
}

Is it valid to set the class member variable like this or is it random behaviour when int x=2 goes out of scope?! Thanks for your help

Another question: In this example

class Test
{
public:
    Test(){m_x = 0;};
    ~Test() {};
    void init(const int x, const int y)
    {
        AnotherClassRessource res(x,y);
        m_other.reset(new AnotherClass(res));
    }
    void doSomething()
    {
        m_other->doSomething();
    }
private:
    int m_x;
    std::unique_ptr<AnotherClass> m_other;
};

int main()
{
    Test X;
    X.init(1,2);
    X.doSomething();
return 0;
}

Is it valid in the void init class function to create a local AnotherClassRessource and pass it as argument to create a new AnotherClass or will it be undefinded behaviour?! It does depend if AnotherCLass uses internaly an reference or pointer to the AnotherClassRessource, doesnt it. Thanks for your help

2
  • It would be invalid only if m_x was a reference. Note that right now you are actually assigning the x argument of setX function which goes out of scope even earlier, not local variable x of SetX. Commented Mar 30, 2020 at 9:02
  • Ok, i think James and wychmaster answered the Edit question, thanks for help. I currently struggle with such things but getting clearer. Commented Mar 30, 2020 at 10:02

3 Answers 3

1

Yes your code is valid, here's a step through of what's happening in your SetX(...) function

void SetX(Test& test)
{ // Start a scope
    int x = 2;    // Create a local variable on the stack named x, set it's value to 2
    test.setX(x); // Pass the local variable by value (does a copy) to the member function setX(...)
} // Deallocate stack variables in this scope (local variable x deallocated here)

So in summary, your local variable x is assigned a value (2) before being passed by value into setX(...) as an argument which means the value of xis copied to the parameter variable x in this code:

void setX(const int x) {m_x=x;}

Addressing your concern: What would not have worked is if your setX(...) member function took and stored a reference to an integer rather than a value. This would mean a reference to a deallocated stack variable could be stored in your class (despite no longer existing).

class Test
{
public:
    void setX(int& x) {m_x=x;} // Notice the & for reference
...
private:
    int& m_x; // Notice the & for reference
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your explanation. I think you answered my editted question aswell :D
@LonelyDriver Then mark his answer as the accepted one by clicking on the hook on the left-hand side. ;)
But it is valid if only the member function setX would take a reference and not the private member m_x?
If the member function setX took a reference but the member variable m_x was not a reference type, then during setX the value of the reference function parameter would be copied to the member variable m_x during the function call, hence before the local variable x is deallocated. The problem only occurs when you're storing a reference or pointer to something deallocated.
0

It is definitely valid, though a bit redundant to have a method to set a variable and then a function which calls the method.

edit: You are probably asking if the instance of your class retains the value you assign to it in your function. That is also true, as you are passing the instance by reference.

1 Comment

Ok it is valid becaus the value of the variable is copy to the memory allocated by the class member variable... ?!
0

If I am not overlooking something, your code should be valid. You only have to worry about variables that go out of scope if you are storing a reference or pointer to them.

However, since m_x is defined as an int and not as an int& or int* you create a copy in your setX class method when you use m_x=x;

So after your function returns, the value is already stored in your class and everything is fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.