C++ 如何在构造函数中初始化结构数据成员?
我使用的是C++ 如何在构造函数中初始化结构数据成员?,c++,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 d
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;
Node*Node;
定位;
它们已经为默认的coinstructor初始化(应该是=default
)
然后只需使用初始化列表:
data::data(std::string loc, int year, int month):loc(std::move(loc)), year(year), month(month) {
}
适当地包括字符串:
#include <string>
#包括
在构造函数中初始化数据的操作如下:
data::data() :
location(""), year(0), month(0)
{
}
data::data(std::string loc, int year, int month) :
location(loc), year(year), month(month)
{
}
#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);
}
#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;
在其他一些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();
}
#包括
#包括“data.h”
int main()
{
//初始化
数据对象(“纽约”,2018年11月);
//使用
std::cout在“data.cpp”中,您可以如下初始化成员:
data::data() :
location(""), year(0), month(0)
{
}
data::data(std::string loc, int year, int month) :
location(loc), year(year), month(month)
{
}
#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);
}
#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;
当您在其他cpp文件中使用该结构时,可以使用如下所示:
data::data() :
location(""), year(0), month(0)
{
}
data::data(std::string loc, int year, int month) :
location(loc), year(year), month(month)
{
}
#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);
}
#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;
如果你开始编码,不要把代码分成太多的文件
除此之外,了解结构成员初始化。或者在
对于成员访问,当指针指向对象时使用箭头运算符,而直接指向对象时使用点运算符。如需进一步阅读:
下面是示例代码:
#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';
}
而且,由于您可能对实现某种数据结构感兴趣,您可能还想了解手动内存管理中的对象生存期和所有权问题。因此,以下是一些后续教育的参考资料:
相关的可能重复:在print.cpp it sys“data”中没有成员“location”感谢您的帮助,您能否澄清在初始化时如何在data.cpp文件中使用对不起,“在初始化时使用data.cpp”是什么意思?例如在temp.hdata d;节点中(std::string id,int year,int month);在temp.cpp Node::Node(std::string id,int year,int month)和print.cpp中,我想获取位置节点*n;coutlocation;
它说“数据”没有成员“位置”,我可以共享print.cpp和temp.h以及temp.cpp