struct within a struct and struct constructor

i have a question about constructor here is an example
struct time
{
int min
int hour;
int seconds;
};
struct person
{
string lastName;
string firstName;
person born
person()//constructor
};
person::person()
{
lastName ="";
firstName="";
};
i dont get how to initialize the constructor of a struct with the struct
for example "born" how do you initialize that data could someone explain it to me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
struct time
{
   time();
   time(int min, int hour, int seconds);
   int min
   int hour;
   int seconds;
};

struct person
{
   string lastName;
   string firstName;
   time born;
   person();
};

person::person()
{
   // No need, strings are initialized with empty strings
   // lastName ="";
   // firstName="";
}

time::time(int min, int hour, int seconds)
{
    this->min = min;
    time::hour = hour;
    this->seconds = seconds;
}

time::time()
{
    min = hour = seconds = 0;
}
Pattako:
prefer member-initialization to assignment statements:
http://stackoverflow.com/questions/7350155/initialisation-and-assignment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
time::time()
{
    min = hour = seconds = 0;
}
//instead this can be done directly as part of the struct declaration:
#include <iostream>

struct time
{
   time(){}
   time(int min, int hour, int seconds);
   int min = 8;
   int hour = 11;
   int seconds = 45;
};
int main()
{
    time t;
    std::cout << t.min << " " << t.hour;
}
/*Program Output:
8 11 */


also const qualify the variables here:time(int min, int hour, int seconds);
> i dont get how to initialize the constructor of a struct with the struct
> for example "born" how do you initialize that data could someone explain it to me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>

struct A { int i ; double d ; std::string s ; };

struct B
{
    A a { 12, 34.56, "hello world" } ; // in-class member initialiser
};

struct C
{
    A a {} ; // in-class member initialiser: value initialised
             // (a.i = 0 ; a.d = 0 ; a.s is default constructed)
};

struct D
{
    // constructor with member initialiser list
    D( int ii, double dd, std::string ss ) : a{ ii, dd, ss } {}

    A a ;
};



> prefer member-initialization to assignment statements

Debatable at best when the members involved are scalars.
If there must be a guideline/rule for non-const scalars, it should be:
favour assignment within the body of the constructor.

1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdlib>

struct A { int rp1 ; int r ; A() : r( std::rand() % 100 ), rp1(r+1) {} }; // dependancy on member declaration order

struct B { int rp1 ; int r ; B() { r =  std::rand() % 100 ; rp1 = r+1; } }; // no dependancy on member declaration order

int main()
{
    A a ; // undefined behaviour
    
    B b ; // fine
}

http://coliru.stacked-crooked.com/a/62e7c447e33a79ff
so for the born object i will not initialize it at all for example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct time
{
int min
int hour;
int seconds;
time();
};
struct person
{
string lastName;
string firstName;
int weight//for the example
person born
person()//constructor
};
time::time()
{
    min =0;
   hour=0;
  seconds=0;
}
person::person()
{
   weight =0;


}

would it be something like this
Last edited on
This is an error; we can't have a non-static member of an incomplete type.
1
2
3
4
5
6
7
8
struct person
{
    // ...
    
    person born ; // *** error: at this point, person has an incomplete type
    
    // ...
};

http://coliru.stacked-crooked.com/a/2c2ed38c6dbb3a9b


This is perhaps what you intended:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct time
{
    int min ;
    int hour ;
    int seconds ;
    time() { min = hour = seconds = 0 ; }
};

struct person
{
    // ...
    int weight ;
    time time_of_birth ;

    person() { weight = 0 ; }
};

In this case, since the constructor of person does not explicitly initialise time_of_birth and time has a non-trivial default constructor, the object would be default constructed.
(The default constructor would set the members min, hour and seconds to zero.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct time
{
    int min ;
    int hour ;
    int seconds ;
    time() { min = hour = seconds = 0 ; }//initialize
};

struct person
{
    // ...
    int weight ;
    time time_of_birth ;

    person() { weight = 0 ; }//initialize
};
time::time()
{
   //do i need to initialize it here also
}

would i still need to initialize it here were i comment at
yea that was a mistake i made was typing too fast, it was suppose to be time born;
If the constructor is already defined inline:
1
2
3
4
5
6
7
struct time
{
    int min ;
    int hour ;
    int seconds ;
    time() { min = hour = seconds = 0 ; } // inline definition of constructor 
};

don't define it again.


If the constructor is declared, but not defined within the class:
1
2
3
4
5
6
7
struct time
{
    int min ;
    int hour ;
    int seconds ;
    time() ; // declaration of constructor 
};

define it out of line
1
2
3
4
time::time()
{
   min = hour = seconds = 0 ; 
}



In this case, the simplest option is to declare in-class member intialisers and not do anything else.
1
2
3
4
5
6
struct time
{
    int min = 0 ;
    int hour = 0 ;
    int seconds = 0 ;
};
thank you for helping me
Topic archived. No new replies allowed.