使用局部变量在函数内设置 Class 成员
Setting Class members within function with local variables
嗨,我有这个例子。
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;
}
像这样设置 class 成员变量是否有效,或者当 int x=2 超出范围时它是随机行为吗?!
感谢您的帮助
另一个问题:
在这个例子中
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;
}
在 void init class 函数中创建本地 AnotherClassRessource 并将其作为参数传递以创建新的 AnotherClass 是否有效,或者它会是未定义的行为吗?!
它确实取决于 AnotherCLass 是否在内部使用指向 AnotherClassRessource 的引用或指针,不是吗。
感谢您的帮助
这绝对是有效的,尽管有一个设置变量的方法然后调用该方法的函数有点多余。
编辑:您可能会问 class 的实例是否保留了您在函数中分配给它的值。这也是正确的,因为您正在通过引用传递实例。
如果我没有漏掉什么,你的代码应该是有效的。如果您正在存储指向它们的引用或指针,您只需要担心超出范围的变量。
但是,由于 m_x
被定义为 int
而不是 int&
或 int*
,因此您在 setX
[=使用 m_x=x;
时的 22=] 方法
所以在你的函数 returns 之后,值已经存储在你的 class 中,一切都很好。
是的,您的代码是有效的,这是您的 SetX(...)
函数
中发生的事情的一个步骤
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)
所以总而言之,您的局部变量 x
在作为参数按值传递到 setX(...)
之前被分配了一个值 (2),这意味着 x
的值被复制到此代码中的参数变量 x
:
void setX(const int x) {m_x=x;}
解决您的问题:
如果您的 setX(...)
成员函数采用并存储对整数而不是值的引用,那将不起作用。这意味着对释放的堆栈变量的引用可以存储在您的 class 中(尽管不再存在)。
class Test
{
public:
void setX(int& x) {m_x=x;} // Notice the & for reference
...
private:
int& m_x; // Notice the & for reference
}
嗨,我有这个例子。
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;
}
像这样设置 class 成员变量是否有效,或者当 int x=2 超出范围时它是随机行为吗?! 感谢您的帮助
另一个问题: 在这个例子中
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;
}
在 void init class 函数中创建本地 AnotherClassRessource 并将其作为参数传递以创建新的 AnotherClass 是否有效,或者它会是未定义的行为吗?! 它确实取决于 AnotherCLass 是否在内部使用指向 AnotherClassRessource 的引用或指针,不是吗。 感谢您的帮助
这绝对是有效的,尽管有一个设置变量的方法然后调用该方法的函数有点多余。
编辑:您可能会问 class 的实例是否保留了您在函数中分配给它的值。这也是正确的,因为您正在通过引用传递实例。
如果我没有漏掉什么,你的代码应该是有效的。如果您正在存储指向它们的引用或指针,您只需要担心超出范围的变量。
但是,由于 m_x
被定义为 int
而不是 int&
或 int*
,因此您在 setX
[=使用 m_x=x;
所以在你的函数 returns 之后,值已经存储在你的 class 中,一切都很好。
是的,您的代码是有效的,这是您的 SetX(...)
函数
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)
所以总而言之,您的局部变量 x
在作为参数按值传递到 setX(...)
之前被分配了一个值 (2),这意味着 x
的值被复制到此代码中的参数变量 x
:
void setX(const int x) {m_x=x;}
解决您的问题:
如果您的 setX(...)
成员函数采用并存储对整数而不是值的引用,那将不起作用。这意味着对释放的堆栈变量的引用可以存储在您的 class 中(尽管不再存在)。
class Test
{
public:
void setX(int& x) {m_x=x;} // Notice the & for reference
...
private:
int& m_x; // Notice the & for reference
}