the : operator
-
Could somebody explain how this works and what it does. I know it can be used to declare a base class, but I have now come across it in another location and I can't work out what it does.
class Error { public: Error(const int nErrCode, char* szErrMess); Error(const Error& e); private: int mnErrCode; char* mpszErrMess; }; Error::Error(const int nErrCode,char* szErrMess) : mnErrCode(nErrCode) { } Error::Error(const Error& e) : mnErrCode(e.mnErrCode) { }
-
Could somebody explain how this works and what it does. I know it can be used to declare a base class, but I have now come across it in another location and I can't work out what it does.
class Error { public: Error(const int nErrCode, char* szErrMess); Error(const Error& e); private: int mnErrCode; char* mpszErrMess; }; Error::Error(const int nErrCode,char* szErrMess) : mnErrCode(nErrCode) { } Error::Error(const Error& e) : mnErrCode(e.mnErrCode) { }
Hi, I believe you mean the two following occurances
waldermort wrote:
Error::Error(const int nErrCode,char* szErrMess) : mnErrCode(nErrCode) ... Error::Error(const Error& e) : mnErrCode(e.mnErrCode)
When using the colon ':' after a constructor you open an initialisation list. This means that member like mnErrCode are initialised with the given error code even before you enter the scope of the constructor.
codito ergo sum
-
Hi, I believe you mean the two following occurances
waldermort wrote:
Error::Error(const int nErrCode,char* szErrMess) : mnErrCode(nErrCode) ... Error::Error(const Error& e) : mnErrCode(e.mnErrCode)
When using the colon ':' after a constructor you open an initialisation list. This means that member like mnErrCode are initialised with the given error code even before you enter the scope of the constructor.
codito ergo sum
So it's basically the same as placing
mnErrCode = nErrCode;
inside the c'tor? I have to ask, what is the point of doing this? -
So it's basically the same as placing
mnErrCode = nErrCode;
inside the c'tor? I have to ask, what is the point of doing this?In this case, probably just convenience. But lets say you have a member that is a class object that doesn't have a default constructor.
-
So it's basically the same as placing
mnErrCode = nErrCode;
inside the c'tor? I have to ask, what is the point of doing this?supose the following situation
class A { public: A(int i) { _i = i; } int _i; }; // class a has no default constructor class B { public: B() {} A _a; }; // the Class B won't compile because there is no default constructor for _a // solution the initialisation list class C { public: C() : _a(1) {} A _a; };
Now when maken an object of the type C the default constructor (of C) will initialize _a and calling the correct constructor of the member _a You might want to check the following http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6[^]
codito ergo sum
-
supose the following situation
class A { public: A(int i) { _i = i; } int _i; }; // class a has no default constructor class B { public: B() {} A _a; }; // the Class B won't compile because there is no default constructor for _a // solution the initialisation list class C { public: C() : _a(1) {} A _a; };
Now when maken an object of the type C the default constructor (of C) will initialize _a and calling the correct constructor of the member _a You might want to check the following http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6[^]
codito ergo sum
Ahhh, I understand perfectly now. Thankyou for the explanation. Infact I now remember seeing and using this before, when using STL containers within a class, I remember first having to initialize them like this before being able to use them. -- modified at 20:20 Friday 25th August, 2006