Quote:Original post by Ignifex
Just one final question:
Why is the vector set outside the bracelets? Can't it also just be set inside of them?
Initializer lists are more efficient in some cases. In most cases, it's trivial, but sometimes, you could cause a major performance loss by not using an initializer list. Basically, if you have a class that has a complex default constructor as a member, then when you construct the outer class, the inner member class's default constructor will get called, which could take a while. Then, after doing the default construction, you might do other stuff that member object that takes more time. With an initializer list, however, the default constructor isn't used, and it skips to initializing it precisely how it needs to be. Consider this example:
class CInside{ public: CInside(const std::string& FileName = "Default.dat"); void GetFileData(const std::string& FileName); private: std::vector<char> mData;};CInside::CInside(const std::string& FileName){ GetFileData(FileName);}void CInside::GetFileData(const std::string& FileName){ //Open file, and read in all data into mData, then close file}class COutside{ public: COutside(const std::string& FileName); private: CInside mInside;};COutside::COutside(const std::string& FileName){ mInside.GetFileData(FileName);}
What will happen here is that when you create a COutside object, the mInside member will get its default constructor called, thus opening "Default.dat" and reading all the data in. This could take a while if it is a large file. Then, immediately after doing all that, the COutside constructor calls GetFileData(), and another file is opened and read. Twice as much work done, when it isn't needed. If you change the COutside constructor to this, though:
COutside::COutside(const std::string& FileName) : mInside(FileName){}
then it will call mInside's constructor with the proper filename, and totally skips the default step. More efficient.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke