c++ - What is this weird colon-member (" : ") syntax in the constructor? -
recently i've seen example following:
#include <iostream> class foo { public: int bar; foo(int num): bar(num) {}; }; int main(void) { std::cout << foo(42).bar << std::endl; return 0; }
what strange : bar(num)
mean? somehow seems initialize member variable i've never seen syntax before. looks function/constructor call int
? makes no sense me. perhaps enlighten me. and, way, there other esoteric language features this, you'll never find in ordinary c++ book?
it's member initialization list. should find information in good c++ book.
you should, in cases, initialize member objects in member initialization list (however, note exceptions listed @ end of faq entry).
the takeaway point faq entry that,
all other things being equal, code run faster if use initialization lists rather assignment.
Comments
Post a Comment