-6

I understand how a basic struct works in C++, as in this example:

struct Options 
{
   int num_particles;
   bool use_lbp;
   string infile;
   string outfile;
};

However, I do not understand the following, where in the declaration there is an additional section. What is the purpose of Options():...?

struct Options 
{
   Options()
      :num_particles(NUM_PARTICLES),
       use_lbp(false),
       infile(),
       outfile()
   {}

   int num_particles;
   bool use_lbp;
   string infile;
   string outfile;
};

Is this similar to what is going on in the code below?

struct State_
{
   State_( State pp ) : p( pp ) { }
   operator State() { return p; }
   State p;
};
Jongware
  • 22,200
  • 8
  • 54
  • 100
RuiQi
  • 488
  • 2
  • 14
  • 8
    A [good introductory book](http://stackoverflow.com/questions/388242) will tell you about constructors, and other basic language features. – Mike Seymour May 26 '15 at 08:32
  • 1
    It's a member function, just like in a class. – Barmar May 26 '15 at 08:32
  • 2
    Do you know what those do for classes? – Luchian Grigore May 26 '15 at 08:33
  • 1
    The only difference between a `struct` and a `class` in C++ is the default visibility of members, in a `struct` it's `public` while in a `class` it's `private`. – Some programmer dude May 26 '15 at 08:33
  • In all honesty, I am familiar with both classes and structures but that `Options:` construction is not something I've seen before. It looks like a constructor which calls a number of other functions – except they are struct members? – Jongware May 26 '15 at 08:36
  • 7
    @Jongware: You've not seen an [initialiser list](http://en.cppreference.com/w/cpp/language/initializer_list) before? That's how the constructor specifies how each base class and member is initialised. – Mike Seymour May 26 '15 at 08:38
  • @Mike: nope :) Coming from C, I tend to treat classes as "structs with functions". Never crossed my mind that structs themselves gained something as well. (Good link though. Mental note: have to read.) – Jongware May 26 '15 at 08:40
  • 1
    I think the most important point is that there is no other way to initialize a struct or class that has a reference or a const qualified member but using the initialiser list – DarthB May 26 '15 at 08:43
  • @DarthB well these days you can also initialize them with a brace-or-equal initializer, but that's just a new syntax for the same thing. – eerorika May 26 '15 at 08:48
  • @DarthB If the struct were an aggregate (i.e. if the constructor were removed), it could also be initialized with an initialization list. – juanchopanza May 26 '15 at 08:52
  • Ahh you mean my_struct = {a_ref, a_val1, ..., a_val2}; will also work!? Did not think about that, good point. – DarthB May 26 '15 at 09:07
  • @JoachimPileborg this is not the only difference. With classes inheritance is private by default (public with structs) – 463035818_is_not_an_ai May 26 '15 at 09:48
  • @Haziq: I reworded the question to remove the asking for "website that I can visit, videos I can watch (etc.), which was (I think) the primary reason for closure. I made the question focus on *this single construction* instead, so it's a better fit for Stack Overflow's Q&A model. (You may want to read the [tour] some time.) – Jongware May 26 '15 at 09:49

1 Answers1

5

Options() is your default constructor. Structs and classes are equivalent in the sense that they can have methods and constructors. What's after : is the initialisation list for Options(). It tells you that in your default constructor for Options():

  • num_particles is initialised to NUM_PARTICLES
  • ule_lbp is initialised to false
  • infile initialised to an empty string
  • outfile initialised to an empty string

Similar line of reasoning applies for State_.

banach-space
  • 1,781
  • 1
  • 12
  • 27