Class construction with initial values

后端 未结 8 1908
别那么骄傲
别那么骄傲 2020-12-06 20:05

I\'m new to C++, and the whole idea of classes - I\'m still reading a book to try and learn. The book I\'m reading says that when I construct a class, I can assign default v

8条回答
  •  孤街浪徒
    2020-12-06 20:28

    Well, this is a typical FAQ question: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6

    In your case, using char and int there are no differences.

    General rule: use initialization list as possibile, there are very few cases when you can prefer assignment, for example for improve readability:

    MyClass::MyClass
    {
      a = b = c = d = e = f = 0;
    }
    

    is better than

    class MyClass::MyClass : a(0), b(0), c(0), d(0), e(0), f(0) { }
    

提交回复
热议问题