0
class a
{
private:
    const int anInt;
public:
    a() : anInt(10) {}
};

vs

class a
{
private:
    const int anInt = 10;
public:
    a() {}
};

What's the difference between these two? When I compile it doesn't seem to have any problem and produce the same results.

Darren
  • 11
  • 2

2 Answers2

0

it's quite different. The Initializer List is used to initialize a class's data members. At some time after the variable is created, 'Assignment' gives that variable a value.

fygeng go
  • 15
  • 4
0

There could be other differences, but the first and main difference between the two is that initialization gives a variable an initial value at the time it is created. On the other hand, assignment gives a variable a value at some point after the variable is created.