0

What exactly is the difference between these two: both are ints int x;

x(0) 

and

int x = 0

Are they equivalent and if not why?

EDIT:

It's to deal with inheritance. You've got something looking like this:

class A{
protected:

int x;

public:

A() : x(0) {};
void show  {cout << "x" << x;     }



};



Class B {
protected:
int y;
public:
B()  {x = 0; y = 2; }

void show  {cout << "x" << x << "y" << y;    }


};

Is the x(0) legal? Please clarify.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631

1 Answers1

1

No difference, also in C++0X: X{0}

And technically they are not cast types, they are initialization or construction.

int x = 0 will call int(0) so it is a constructor call and not assignment.

Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80