Is there a difference between Demo1 and Demo2 in the following code?
#include <iostream>
class Base {};
class Demo1 : public Base {
public:
Demo1() : instance_variable(5) {
}
int instance_variable;
};
class Demo2 : public Base {
public:
Demo2() {
this->instance_variable = 5;
}
int instance_variable;
};
int main(int argc, char **argv)
{
Demo1 a;
Demo2 b;
std::cout << "a.instance_variable: " << a.instance_variable << std::endl;
std::cout << "b.instance_variable: " << b.instance_variable << std::endl;
return 0;
}
I saw the first notation in a book I am reading right now, and I am not sure if the two are equivalent or if Demo1 has subtle differences I should be aware of.
I find the second much more readable as I am coming from a Java/JavaScript background, so I am curious if I can replace "Demo1" with "Demo2" without changing the meaning of the code.
Also, what is the expression "constructor:field(x){}" called?