How can I initialize struct data members in Constructor?

c++ struct constructor initialization
c++ default member initialization
c++ struct default constructor
c++ struct initialization
c++ constructor initialization list
c++ struct default initialization
c struct initialization list
c++ struct constructor with parameters

I am using data.h file which have the following code

#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 this

#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.

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;

They are already initialized for the default coinstructor (which shoudl proably be =default instead).

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>

c++ - How can I initialize struct data members in Constructor?, and the data.cpp file looks like this #include "data.h" #include "string" using namespace std; data::data() { //initialize the data members (location  Constructor is a special non-static member function of a class that is used to initialize objects of its class type.. In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual bases and non-static data members.

in your "data.cpp", you can initialize the members like this:

#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:

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

Constructors and member initializer lists, In the definition of a constructor of a class, member initializer list struct S { int n; S(int); // constructor declaration S() : n(7) {} // constructor definition. For bases and non-static data members that cannot be default-initialized,  Non-static data members - cppreference.com, All of member variables were properly initialized before our constructor was called. Note, that we did not initialize members in the constructor. Such approach is not Commonly, you make data member static so you don't need to create an instance to be able to access that member.

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:

#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();
}

Should structs Have Constructors in C++, An aggregate initializer is a set of data between braces, that you can use to initialize the data member of struct . For instance, with this struct:. We can initialize an instance with the following syntax: Point p = {1, 2, 3}; 1. Point p = {1, 2, 3}; This instance p then has its x equal to 1, its y equal to 2 and its z equal to 3. Note that since C++11, we can also write it without the equal sign: Point p {1, 2, 3}; 1. Point p {1, 2, 3};

Constructors and Destructors, 'Car::name' : only static const integral data members can be initialized within a class We can create constructors by using the class or struct name as a function  If you do not explicitly initialize a base class or member that has constructors by calling a constructor, the compiler automatically initializes the base class or member with a default constructor. In the above example, if you leave out the call B2() in the constructor of class D (as shown below), a constructor initializer with an empty

Structures and Member Initializers in C++, Since C++11, one can put the initialization of data members directly into structure or class Indeed, by adding member initializers into a structure may render your user-provided constructors, base classes, or virtual member  Initialize a structure with input parameter i am newbie at c++ and i have problem initializing my container occ_stat_t using the member function CNetwork::CNetwork. i guess that something is wrong at line: occ_stat[0]( num_elements_ ) , occ_stat[1]( num_elements_ ) but i dont really know how t

Initialization in C++ is Seriously Bonkers, A smellier way would be to set the value inside the constructor body: struct A { A() { i = 0; } int i; }; Because the constructor body can pretty much do anything, it's better to separate initialization into the member initializer list (technically a part of the constructor body). readonly instance members. Beginning with C# 8.0, you can also use the readonly modifier to declare that an instance member doesn't modify the state of a struct. If you can't declare the whole structure type as readonly, use the readonly modifier to mark the instance members that don't modify the state of the struct.

Initialization of base classes and members (C++ only), Constructors can initialize their members in two different ways. Both methods assign the argument values to the appropriate data members of the class. struct A { int k; A(int i) : k(i) { } }; struct B: A { int x; int i; int j; int& r; B(int i): r(x), A(i),  Since C++11, one can put the initialization of data members directly into structure or class definition. However, this change has consequences with regards to the construction of such structures. Indeed, by adding member initializers into a structure may render your previously working code non-compilable.

Comments
  • Possible duplicate of C++ Member Initialization List
  • Related: stackoverflow.com/q/926752/1025391
  • in print.cpp it sys 'data' has no member 'location'
  • thanks for your help can you please clarify that how can i use in data.cpp file at the time of initialization
  • Sorry, what do you mean "use data.cpp at time of initialization"?
  • for example in temp.h data d; Node(std::string id, int year, int month); and in temp.cpp Node::Node(std::string id, int year, int month) and in print.cpp I want to get a location Node * n; cout<<n->data->location;
  • it says "data" has no member "location" I can share print.cpp and temp.h and temp.cpp