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

时间:2018-11-14 11:29:52

标签: c++

我正在使用具有以下代码的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

data.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)
}

在其他.cpp文件中,如何获取这些值并初始化这些值。

node.h

struct Node {
data d;

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

};

node.cpp

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

}

print.cpp

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

4 个答案:

答案 0 :(得分:3)

它们已经被初始化为默认的coinstructor(应该将其替换为=default。)

然后只使用初始化列表:

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

也正确包含字符串:

#include <string>

答案 1 :(得分:1)

在“ 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);
}

当您在其他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;

答案 2 :(得分:0)

在构造函数中初始化数据的步骤如下:

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

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

在其他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();
}

答案 3 :(得分:0)

不要过于复杂。如果您开始编码,请不要将代码分成太多文件。

除此之外,了解有关结构成员初始化的信息。 on this siteyour favorite documentation page

对于成员访问,使用指向对象的指针时使用箭头运算符,而直接拥有对象时使用点运算符。进一步阅读:

下面是示例代码:

#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';
}

输出为:

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

由于似乎您可能正在实现某种数据结构,因此您可能还想了解有关对象生存期和手动内存管理的所有权问题。因此,一些继续教育的参考资料: