简体   繁体   English

如何在构造函数中初始化结构数据成员?

[英]How can I initialize struct data members in Constructor?

I am using data.h file which have the following code我正在使用具有以下代码的data.h文件

#ifndef __DATA_h_INCLUDED__
#define __DATA_h_INCLUDED__

#include "string"

struct data {
    std::string location="";
    int year = 0, month = 0;

    data();
    data(std::string location, int year, int month);
};

#endif

and the data.cpp file looks like thisdata.cpp文件看起来像这样

#include "data.h"
#include "string"

using namespace std;

data::data() {
    //initialize the data members (location,year,month)
} 

data::data(std::string loc, int year, int month) {
    //initialize the data members (location,year,month)
}

in some other .cpp file how can i get these values and initialize these values.在其他一些 .cpp 文件中,我如何获取这些值并初始化这些值。

node.h节点.h

struct Node {
data d;

Node(std::string id, int year, int month); 

};

node.cpp节点.cpp

Node::Node(string id, int year, int month){
// here i want to initialize 'data' 

}

print.cpp打印文件

Node* node;
cout<<node->data->location;

They are already initialized for the default coinstructor (which shoudl proably be =default instead).它们已经为默认的 coinstructor 初始化(应该是=default )。

Then just use the initialization list:然后只需使用初始化列表:

data::data(std::string loc, int year, int month):loc(std::move(loc)), year(year), month(month) {
}

Include string properly as well:也正确包含字符串:

#include <string>

in your "data.cpp", you can initialize the members like this:在您的“data.cpp”中,您可以像这样初始化成员:

#include "data.h"
#include "string"
using namespace std;
data::data() : year(0), month(0) {
    //initialize the data members (location,year,month)
    //in fact, 'location' donot need initialization, 
    //because the member will be constructed first as 
    //a empty string before give control to user-defined constructor.
    location = "";
} 
data::data(std::string loc, int _year, int _month)
    year(_year), month(_month) {
    //initialize the data members (location,year,month)
    location = loc; // or location.assign(loc);
}

when you use the structure in other cpp file, you may use is like this:当您在其他 cpp 文件中使用该结构时,您可能使用的是这样的:

#include "data.h"
data x; //call default constructor: data();
//since struct 's member is implicitly public, 
//you can access them from outside of its defination.
x.location = "your location";
x.location.assign("some other place");
x.location.append("etc");
x.year = 2018;
x.month = 11;

Initializing data in constructor is done like this:在构造函数中初始化数据是这样完成的:

data::data() : 
    location(""), year(0), month(0)
{
} 

data::data(std::string loc, int year, int month) : 
    location(loc), year(year), month(month)
{
}

In some other cpp file, for example, main.cpp, you can use this like that:在其他一些 cpp 文件中,例如 main.cpp,您可以这样使用:

#include <iostream>    
#include "data.h"

int main()
{    
// initializing
    data obj("NY", 2018, 11);

// using  
    std::cout << "Year:  " << obj.year << std::endl;
    std::cout << "Month: " << obj.month << std::endl;
    std::cout << "Loc:   " << obj.location << std::endl;

// setting properties
    obj.year = 2100;
    obj.month = 1;
    std::cout << "Year:  " << obj.year << std::endl;
    std::cout << "Month: " << obj.month << std::endl;

// initializing by default values
    data obj2();
}

Don't overcomplicate this.不要把这个复杂化。 If you start coding, don't split your code up in too many files.如果您开始编码,请不要将您的代码分成太多文件。

Other than that, learn about struct member initialization.除此之外,了解结构成员初始化。 Either on this site or at your favorite documentation page .无论是在这个站点上,还是在您最喜欢的文档页面上

For member access you use the arrow-operator when you have a pointer to an object, vs. the dot-operator that you use when you have the object directly.对于成员访问,当您拥有指向对象的指针时使用箭头运算符,而当您直接拥有对象时使用点运算符。 For further reading:进一步阅读:

Here's example code:这是示例代码:

#include <iostream>

struct data {
    int x_;
    // use member initialization list to init the data value directly
    data(int x) : x_(x) {}
};

struct node {
    data data_;
    // use member initialization list to init the data value directly
    node(int x) : data_(x) {}
};

int main() {
    // create object
    node n(42);
    // acquire pointer to object
    node *p = &n;
    // use arrow to access member with pointer, use dot to access with object
    std::cout << p->data_.x_ << '\n';
}

The output is:输出是:

$ g++ test.cc && ./a.out
42

And because it seems you might be into implementing some sort of data structure, you might also want to learn about object lifetime and ownership issues with manual memory management.并且因为看起来您可能正在实现某种数据结构,您可能还想通过手动内存管理了解对象生命周期和所有权问题。 So some references for further education:所以一些继续教育的参考:

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

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