Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 没有参数的默认构造函数是否总是初始化变量?_C++_Visual C++ - Fatal编程技术网

C++ 没有参数的默认构造函数是否总是初始化变量?

C++ 没有参数的默认构造函数是否总是初始化变量?,c++,visual-c++,C++,Visual C++,我有一个类,它包含一个默认构造函数,该构造函数应该初始化几个私有成员变量。我想知道我的变量是否被初始化过,因为当我在类方法中调用变量时,没有打印任何内容。有人能给我解释一下吗?我的代码如下: class Practice_Class { public: Practice_Class() { a = 6; b = 89; c = 45; } void variables_in_class() { cout << a << " "<<

我有一个类,它包含一个默认构造函数,该构造函数应该初始化几个私有成员变量。我想知道我的变量是否被初始化过,因为当我在类方法中调用变量时,没有打印任何内容。有人能给我解释一下吗?我的代码如下:

class Practice_Class
{
public:
Practice_Class()
{
  a = 6;
  b = 89;
  c = 45;
}
  void variables_in_class()
  {
    cout << a << " "<< b << " "<< c << " "<< endl;
  }

private:
    int a, b, c;

};


int main()
{
 Practice_Class the_value;
 cout << the_value.variables_in_class();

}
课堂练习\u课堂
{
公众:
实习班()
{
a=6;
b=89;
c=45;
}
类()中的无效变量
{
cout这应该有效:

int main()
{
    Practice_Class the_value;

    // you are outputing in variables_in_class(), not its return values
    the_value.variables_in_class();
}
关于你的问题:

没有参数的默认构造函数是否总是初始化变量

否。如果您在默认构造函数中不执行任何操作,它将自动调用类成员的默认构造函数。此外,它不执行任何操作

我有一个类,它包含一个默认构造函数 初始化几个私有成员变量

您不需要初始化您的。您可以分配它们。以下是使用初始化列表进行初始化的正确方法:

Practice_Class() :
  a(6),
  b(89),
  c(45)
{
}
我想知道我的变量是否被初始化过,因为当我在类方法中调用变量时,没有打印任何内容

代码甚至不应编译。请参见以下内容:

<> P>不过,习惯C++方式是为你的类提供一个相应的输出流过载,这样你就可以把类的实例传递给<代码> STD::CUT或其他输出流。 例如:

#include <iostream>

class Practice_Class
{
public:
    Practice_Class() :
        a(6),
        b(89),
        c(45)
    {
    }

private:
    // so that the function below can access the variables:
    friend std::ostream& operator<<(std::ostream& os, Practice_Class const& obj);

    int a;
    int b;
    int c;
};

std::ostream& operator<<(std::ostream& os, Practice_Class const& obj)
{
    os << obj.a << " " << obj.b << " " << obj.c;
    return os;
}

int main()
{
    Practice_Class the_value;
    std::cout << the_value << '\n';
}
#包括
课堂练习
{
公众:
练习课():
a(6),
b(89),
c(45)
{
}
私人:
//以便下面的函数可以访问变量:

friend std::ostream&OperatorHis compiles?它不是为我编译的。你有打字错误和无效语法。如果我修复了所有编译错误,它就会像我预期的那样工作。删除
cout,那么你现在显示的代码有什么问题吗?在这个简单的情况下,如果我替换
cout,你的程序就会编译如果我删除
cout@MichaelWalz:当然,在这种情况下没有什么区别。我只是想教你一些好习惯。
the_value.variables_in_class();
#include <iostream>

class Practice_Class
{
public:
    Practice_Class() :
        a(6),
        b(89),
        c(45)
    {
    }

private:
    // so that the function below can access the variables:
    friend std::ostream& operator<<(std::ostream& os, Practice_Class const& obj);

    int a;
    int b;
    int c;
};

std::ostream& operator<<(std::ostream& os, Practice_Class const& obj)
{
    os << obj.a << " " << obj.b << " " << obj.c;
    return os;
}

int main()
{
    Practice_Class the_value;
    std::cout << the_value << '\n';
}