0

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.

Utkarsh
  • 59
  • 7
  • 1
    You should use `std::string` instead of `char[]` – Cory Kramer Aug 01 '17 at 12:48
  • 2
    You've managed to skip initializing any of them so far so you're on the right track – systemcpro Aug 01 '17 at 12:50
  • 3
    Sure write a constructor and omit intialization code for the age. – hetepeperfan Aug 01 '17 at 12:50
  • 3
    You need to read a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Passer By Aug 01 '17 at 12:52
  • Possible duplicate of [How do C++ class members get initialized if I don't do it explicitly?](https://stackoverflow.com/questions/3127454/how-do-c-class-members-get-initialized-if-i-dont-do-it-explicitly) – underscore_d Aug 01 '17 at 12:58
  • Another possible duplicate: [C++ Structure Initialization](https://stackoverflow.com/questions/11516657/c-structure-initialization) – underscore_d Aug 01 '17 at 13:15
  • Think about it: In your 2nd example, how is the compiler meant to know that the `70` is for `weight` and not `age`? So, you need to either provide a 2-argument constructor that uses the arguments that way, or alternatively designate which members you want to initialise... and that's the proposed feature of designated initialisers, as discussed at the question linked directly above this comment. – underscore_d Aug 01 '17 at 13:16
  • More simply, why do you think you want to skip initialising `age`? It's generally not a good idea to have any object with an indeterminate value. Do you really want to set it to a sentinel value by default, e.g. -1? – underscore_d Aug 01 '17 at 13:43
  • 1
    Btw, storing a person's age is generally a bad idea, for it changes with time. Better store their date/year of birth. – Walter Aug 01 '17 at 13:50
  • the struct I created in question is just for an example. Practically it doesn't make any sense.But i just wanted to know about the initializing part. I didn't think much while coming up with an example, should've thought of a better example . Anyways the answers were able to help me with my question :) – Utkarsh Aug 01 '17 at 13:57

2 Answers2

1

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;
};
Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • 3
    *"Members are not automatically initialized in C++"* - That's wrong and misleading. Default initialization always occurs for all members that aren't explicitly initialized. That fact that for fundamental types this initialization is a no-op doesn't mean it doesn't occur from a type system perspective. – StoryTeller - Unslander Monica Aug 01 '17 at 13:02
  • Fundamental types _are_ automatically initialised, in the sense that the lifetime of the object begins and it becomes valid to assign-to; it just so happens to have an indeterminate value. – underscore_d Aug 01 '17 at 13:06
1

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
Walter
  • 44,150
  • 20
  • 113
  • 196