0

I'm moving from C to C++ and wondering about this decalaration, adapted from the very introduction to classes in Bjarne Stroustrup's The C++ Programming Language (Fourth Edition)

class myClass {
private:
    int myvar;
public:
    myClass(int i = 0) :myvar { i } { }
};

I get that this defines a constructor for myClass, taking ì as optional argument defaulting to 0, which somewhat is used as the initial value of variable myvar for the new instance of myClass. But I don't get where/how that syntax is defined, and more importantly the difference that makes with:

class myClass {
private:
    int myvar;
public:
    myClass(int i = 0) {
        myvar = i;
    }
};
rookie
  • 1
  • 1
  • 1
    It is about the same difference as between `int a = 0;` and `int a; a = 0;` - the first is generally more safe and efficient – Mat Mar 12 '22 at 08:31

0 Answers0