Utkarsh
Utkarsh

Reputation: 59

Skipping the initialization of a structure member while initializing other members

Is it possible to omit initializing a struct's member while initializing other members?

For example, this is my struct

struct person { 
    std::string name;
    int age;
    float weight;
};

And while initializing it, I want to skip initializing age, but initialize the other members. For example, I create a variable p of type person. This is how I would do it if I wanted to give values to all the members.

person p = {"harry", 25, 70};

Instead, I want to know if it is possible to omit giving the value '25' to age to the person p(just for this particular structure variable) for the meanwhile and just give values to the other two. Like in for loop we can skip any of the parameters by leaving it blank like this

 for(int i=0;;i++)

I know that structs are not anywhere related to loops, I mentioned it just to explain what exactly do i mean by skipping the initialization of age.

Upvotes: 0

Views: 2765

Answers (2)

Walter
Walter

Reputation: 45424

Unfortunately, your question is unclear.

  1. Do you want age to be left uninitialized for every person or only for certain persons?
  2. How will you distinguish a person whose age is initialized from one for which it has been assigned a meaningful value?

As mentioned in the comments, every member will be initialized upon construction, either as specified in the constructor, or as specified in the member declaration, or default-initialized. For fundamental types (such as int), the latter does not set a value, such that the actual value stored can be anything, including non-sensical (NaN for float).

The only reason to keep fundamental types default-initialized (and hence at unspecified value) is for efficiency (e.g. when allocating large numbers). However, that is not the case here. Therefore, you should use for all members default values which are meaningless for real persons (to make clear that they are merely unspecified or 'unknown'). For example

struct person
{
  std::string name;   // default: empty
  int age = -1;       // idea: match age < 0 to 'unknown'
  float weight = 0;   // idea: match weight <= 0 to 'unknown'
  person(std::string const&n) : name(n) {}
  person(std::string const&n, int a) : name(n), age(a) {}
  person(std::string const&n, float w) : name(n), weight(w) {}
  person(std::string const&n, int a, float w) : name(n), age(a), weight(w) {}      
};

person A{Utkarsh};           // unknown age, unknown weight
person B{Utkarsh,70.0};      // unknown age, weight=70
person C{Utkarsh,25};        // age=25, unknown weight
person D{Utkarsh,25,70.0};   // age=25, weight=70

Upvotes: 1

Nikos C.
Nikos C.

Reputation: 51842

Members that are of a fundamental type are not automatically initialized to any specified value in C++, unless the object is of static storage class (like global variables or variables declared static) in which case they are initialized to 0.

So you don't need to do anything special. Just don't initialize members you don't want to have a specific value.

Currently, only name is being initialized to a known value in your struct (because the default constructor of std::string is used.) If you want to initialize everything except age, then:

struct person {
    std::string name; // default constructed to empty string
    int age; // unspecified value
    float weight = 0.f;
};

Upvotes: 1

Related Questions