简体   繁体   English

在C ++中初始化struct的数据成员

[英]Initializing data member of struct in C++

//Not able to figure out how we can initialize data member of an struct inside //struct . //无法弄清楚如何在// struct中初始化结构的数据成员。 I believe this is against the guidelines of OOP . 我认为这违反了OOP的准则。 The below program is not showing any compiler error and runs fines and gives output 10 30 下面的程序未显示任何编译器错误,并且运行正常,并给出输出10 30

#include<iostream>
using namespace std;
struct Accumulator
{
    int counter = 0;
    int operator()(int i) { return counter += i; }
};
int main(void)
{
   Accumulator acc;
   cout << acc(10) << endl; //prints "10"
   cout << acc(20) << endl; //prints "30"
}

The rules regarding in class initialization have changed. 有关类初始化的规则已更改。 Starting in C++11 you can initialize a non static class member directly in the class body. 从C ++ 11开始,您可以直接在类主体中初始化一个非静态类成员。 This is just syntactic sugar though. 虽然这只是语法糖。 When you write 当你写

struct Accumulator
{
    int counter = 0;
    int operator()(int i) { return counter += i; }
};

The compiler will actually add the initialization of counter to the default constructor. 编译器实际上会将计数器的初始化添加到默认构造函数中。 So the above code would get translated to 所以上面的代码将被翻译成

struct Accumulator
{
    int counter;
    Accumulator() : counter(0) {}
    int operator()(int i) { return counter += i; }
};

This initialization is also suppressed if you supply your own initialization. 如果您提供自己的初始化,也会抑制该初始化。 If we had 如果我们有

struct Accumulator
{
    int counter = 0;
    int sum = 0;
    Accumulator() {}
    Accumulator(int counter) : counter(counter) {}
    int operator()(int i) { return counter += i; }
};

Then Accumulator() {} would actually be 然后, Accumulator() {}实际上是

Accumulator() : counter(0), sum(0) {}

and Accumulator(int counter) : counter(counter) {} would be Accumulator(int counter) : counter(counter) {}将是

Accumulator(int counter) : counter(counter), sum(0) {}

That's done with the constructor , to which you can pass the initial value. 这是通过构造函数完成的,您可以将初始值传递给该构造函数

Something like: 就像是:

struct Accumulator
{
    Accumulator(int initial = 0)
        : counter(initial)
    {}

    int counter;

    int operator()(int i) { return counter += i; }
};

int main(void)
{
    Accumulator acc(20);
    std::cout << acc(10) << '\n'; //prints "30"
    std::cout << acc(20) << '\n'; //prints "50"

    // Reset accumulator
    acc = Accumulator();  // Use constructors default argument
    std::cout << acc(10) << '\n'; //prints "10"
    std::cout << acc(20) << '\n'; //prints "30"
}

If you wonder why it's working without the constructor too, that's because the structure without a constructor is an aggregate and you can use aggregate initialization . 如果您想知道为什么在没有构造函数的情况下也可以工作,那是因为没有构造函数的结构是聚合的 ,您可以使用聚合初始化

If you wonder about the initialization of the counter member, it's because it's a non-static data member and since the C++11 standard those can be initialized like that. 如果您想知道counter成员的初始化,那是因为它是一个非静态数据成员,并且由于是C ++ 11标准,所以可以像这样初始化它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM