Just a simple 2D vector

Started by
11 comments, last by Ignifex 19 years, 8 months ago
Quote:Original post by Ignifex
Why is the vector set outside the bracelets? Can't it also just be set inside of them?


Because intialization/construction is different from assigment even if the final result is the same, what i mean is this:
int i;i = 10;


is different from this:
int i(10);or int i = 10; //implicitly invokes constructor


all built-in types and standard library types in c++ have constructors to intialize instances.

The intializer list allows you to use the members constructors to intialize a type's members.

EDIT: with assignment the variable may have had a previous value
Advertisement
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
Thanks Agony/snk_kid, you're both really great!
To Agony:
second point: Yes, I allready realised that, but since the vectors inside the vectors are supposed to start at size 0, that's no problem at all.
third point: So that's why! I should've figured that one out myself. I hardly use < and > operators, so I'm no used to their combination together. Thanks for the explanation here.
To snk_kid:
Ok, right. It all makes sense to me now. Thanks again.

This topic is closed to new replies.

Advertisement