C++类没有返回我的私有变量的正确值

发布于 2024-12-10 12:06:38 字数 842 浏览 1 评论 0原文

我试图让这个程序接受用户输入并将其放入公共函数中并将其分配给 privateVariable,然后我希望它将 privateVariable 的值返回到 main() 并将其输出到屏幕,但它显示的所有内容是未定义 int 的值 (-858993460)。我在这里遇到什么逻辑问题?

#include <iostream>
#include <string>

using namespace std;

class MyClass
{

    private:
        int privateVariable;

    public:
        int userVariable;

    void setVariable(int userVariable)
    {  
        privateVariable = userVariable;             
    } 

    int getVariable()
    {
        return privateVariable;                     
    } 

};

int main()
{
    int userVariable;
    cin >> userVariable;

    MyClass object1;
    MyClass object2;

    object1.setVariable(userVariable);        
    object2.getVariable();                   

    cout << object2.getVariable();            

    system("PAUSE");

    return 0;
}

Im trying to get this program to take the users input and put that into a public function and assign it to the privateVariable, then I want it to return the value of privateVariable to main() and output it to the screen, but all it displays is the value of an undefined int ( -858993460 ). What logical problem am I having here ?

#include <iostream>
#include <string>

using namespace std;

class MyClass
{

    private:
        int privateVariable;

    public:
        int userVariable;

    void setVariable(int userVariable)
    {  
        privateVariable = userVariable;             
    } 

    int getVariable()
    {
        return privateVariable;                     
    } 

};

int main()
{
    int userVariable;
    cin >> userVariable;

    MyClass object1;
    MyClass object2;

    object1.setVariable(userVariable);        
    object2.getVariable();                   

    cout << object2.getVariable();            

    system("PAUSE");

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不会写诗 2024-12-17 12:06:38

您正在 object1 中进行设置并从 object2 中获取。 object1object2 是不同的对象。由于 object2 中的变量未设置,因此您会得到一个垃圾值。

我发现 MyClass 中没有使用 public userVariable

You are setting in object1 and getting from object2. object1 and object2 are different objects. As variable in object2 is not set, you get a garbage value.

And I see no use of public userVariable in MyClass.

随遇而安 2024-12-17 12:06:38

您没有设置变量。您对 object1 调用 setVariable,对 object2 调用 getVariable,因此 object1 的成员仍未初始化。

object1.setVariable(5); // object1.privateVariable = 5
                        // object2.privateVariable -> still uninitialized
object2.getVariable();  // returns uninitialized variable

为此,取决于您想要什么:

class MyClass
{
private:
   static int privateVariable;
//......
}

这样, privateVariable 将是类范围的成员,而不是实例范围的成员。这意味着该类的所有实例都具有相同的值(即使未创建实例)。这也意味着您可以将两个函数设为静态:

class MyClass
{
private:
   static int privateVariable;
public:
   static void setVariable(int userVariable)
   {  
      privateVariable = userVariable;             
   } 

   static int getVariable()
   {
      return privateVariable;                     
   } 
};

并且可以在没有实例的情况下调用方法:

MyClass::setVariable(5); //MyClass.privateVariable = 5;
MyClass::getVariable(); //returns 5
object1.getVariable(); //returns also 5

另一个选择是,如果您不需要静态成员,则为两个对象设置成员:

object1.setVariable(5); // object1.privateVariable = 5
                            // object2.privateVariable -> still uninitialized
object2.setVariable(5); //object2.privateVariable = 5
object2.getVariable();  // returns 5

或者,您可以定义一个构造函数并设置那里的变量:

class MyClass
{
private:
   static int privateVariable;
//......
public:
   MyClass()
   {
      privateVariable = 5;
   }
}

这样,您创建的每个对象都会将成员初始化为 5。

You are not setting the variable. You call setVariable on object1 and getVariable on object2, so the member of object1 remains uninitialized.

object1.setVariable(5); // object1.privateVariable = 5
                        // object2.privateVariable -> still uninitialized
object2.getVariable();  // returns uninitialized variable

For this to work, depending on what you want:

class MyClass
{
private:
   static int privateVariable;
//......
}

This way, privateVariable will be a class-scoped member, not instance-scoped. That means it has the same value for all instances of the class (and even if instances were not created). This also means you can make both your functions static:

class MyClass
{
private:
   static int privateVariable;
public:
   static void setVariable(int userVariable)
   {  
      privateVariable = userVariable;             
   } 

   static int getVariable()
   {
      return privateVariable;                     
   } 
};

and you can call the methods without instances:

MyClass::setVariable(5); //MyClass.privateVariable = 5;
MyClass::getVariable(); //returns 5
object1.getVariable(); //returns also 5

Another option is, if you don't want static members, to set the member for both objects:

object1.setVariable(5); // object1.privateVariable = 5
                            // object2.privateVariable -> still uninitialized
object2.setVariable(5); //object2.privateVariable = 5
object2.getVariable();  // returns 5

Or, you could define a constructor and set the variable there:

class MyClass
{
private:
   static int privateVariable;
//......
public:
   MyClass()
   {
      privateVariable = 5;
   }
}

With this, every object you create will have the member initialized to 5.

↙温凉少女 2024-12-17 12:06:38

当您在 object1 上设置变量时,object2 没有初始化您的变量,您发布的代码仅在 privateVariable 为静态时才有效。

object2 does not have your variable initialized as you set it on object1, the code you posted would only work if privateVariable was static.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文