-2

I can't understand the part of code “Point() : x(0), y(0) {},Point(int a, int b) : x(a), y(b) {}”..

Especially about the question, ":" is used for what here?

thanks in advance!

 Definition for a point.
      struct Point {
          int x;
          int y;
          Point() : x(0), y(0) {}
          Point(int a, int b) : x(a), y(b) {}
      };
Rapptz
  • 20,807
  • 5
  • 72
  • 86
lhuang
  • 58
  • 9
  • thank you, one mark, for a java programmer, the syntax to declare a constructor is really strange. – lhuang Dec 15 '13 at 14:17

3 Answers3

1

it is used to begin an initializer list

x will be initalized with a and y with b in this example.

It is compulsory for const members of a class or a struct to use initialize it through an initializer list

Gabriel
  • 3,564
  • 1
  • 27
  • 49
1

Point() : x(0), y(0) {} <-- this is a constructor that creats a point at (0,0) coordonates. Point(int a, int b) : x(a), y(b) {} this is a constructor that creats a point at (a,b) coordonates. Where a and b are given as input by the user.

t.niese
  • 39,256
  • 9
  • 74
  • 101
CiucaS
  • 2,010
  • 5
  • 36
  • 63
0

It's the start of an initialiser list for the members of the class/struct. You can use it call constructors of the class members, like you're doing there.

splrs
  • 2,424
  • 2
  • 19
  • 29