khanhdungtrinh's blog

By khanhdungtrinh, history, 16 months ago, In English

This code is from: https://codeforces.net/blog/entry/86731 ~~~~~ struct Line { data_t a, b;

Line() : a(0), b(-inf) {} Line(data_t a, data_t b) : a(a), b(b) {}

data_t get(data_t x) { return a * x + b; } }; ~~~~~

I have no idea what the colon marks in the code means?

  • Vote: I like it
  • +2
  • Vote: I do not like it

»
16 months ago, # |
  Vote: I like it +10 Vote: I do not like it

It is used in constructors to initialize member variables of classes/structs. The variables outside the parentheses are member variables and the values within the corresponding parentheses are the values to which you initialize the member variables.

This is generally the preferred method of initializing values in classes/structs when possible.

  • »
    »
    16 months ago, # ^ |
    Rev. 2   Vote: I like it +1 Vote: I do not like it

    One advantage of this synatax is that it allows one to initialise a const variable with some value. This can be helpful if we don't want to change size of a structure after it is initialised.

    Example
  • »
    »
    16 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I got it, thanks.