0

What is the most appropriate way to initialize members of a class using a constructor in C++? Most video tutorials describe the second approach, while I tried the following way and it works fine.

class node
{
public:
    int x, y;
    node( int a, int b ) { x = a; y = b;}
};

And this apporach is commonly used in tutorials or books:

class node {
public:
    int x, y;
    node(int a, int b): x(a), y(b) {}
};

I find the first approach more intuitive.

user3125772
  • 73
  • 1
  • 10
  • 3
    Use the member initializer list. – πάντα ῥεῖ Apr 23 '16 at 06:52
  • Remember in the initialisation list they will get initialised in the order that they are declared. NOT in the order of the initialisation list. – Ed Heal Apr 23 '16 at 06:55
  • 1
    Learn what they both do, and then make your decision. If, after understanding what they both do, you still prefer the first form(for readability, or whatever other reason you have), then go for it. – Benjamin Lindley Apr 23 '16 at 06:55

0 Answers0