Hi
I would like to know which is better. inialise a value like

int i=0;

or like this.

int i(0);

and why.

Recommended Answers

All 5 Replies

For native types I don't think it makes a difference. For user-defined types I think it may mean the difference (if any) between calling (the default constructor and [probably not in this case?]) the assignment operator and a copy constructor.

Real C++ people...?

>I would like to know which is better.
There's no difference if they both compile (they might not for user-defined types), pick which you find more intuitive.

>Real C++ people...?
I don't think I count as a real C++ person, but both call the primary (non-copy) constructor. The only noticeable difference would be if the constructor in question is defined as explicit, in which case an implicit argument would be illegal:

class test1 {
public:
  test1 ( int init ) {}
};

class test2 {
public:
  explicit test2 ( int init ) {}
};

int main()
{
  test1 a1 ( 0 );
  test1 b1 = 0;
  test2 a2 ( 0 );
  test2 b2 = 0; // This will cause an error
}

thank you everyone for the answer

Thanks, I needed that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.