   Three c++ questons
  There''s these three oddities in my collection, hopefully some one could explain them. I ask them here becose I don''t know what to call them so it''s bit tricky to find them in MSDOCS and some sambles of usage are allways well come.1) what does the throw() do after the constructor (I know the basic usage of throw)2) what does the const do after argument list? (I also know this ones usage whit the vars)3) why are people initing variables like this: "func() : mVar(0) {}"  isn''t that same than writing "mVar(0)" or "mVar=0;" inside the function?   class cpp{public: cpp() throw(); void fn(int& i) const;};cpp::cpp(){}void cpp::fn() const {}
  1) No experience there.2) const after the argument list makes the class constant I believe, so you couldnt change the class from within that method. someone correct me if I''m wrong.3) as part of the constructor, you can initialize variables that are part of that class by using a constructor initializer list. Thats that the mVar(0) is. It is slightly faster I believe because you are invoking the constructor of the variable with the initialization value. If you just did mVar = 0 in the constructor body, there is 2 things happening. The default constructor is called during class construction, and then the assignment operator is called when you do your mVar = 0Someone correct me if I''m wrong. ;  1) It means the constructor promises not to throw.2) It means the member function promises not to modify any object data, and can therefore be used on const objects.3) It is an initializer list. The value is directly passed to the base class or member variables constructors. Doing an assignement in the function body means you have first to default-construct the member variable, and then overwrite that value with the new one. If your member variable doesn''t have a default constructor, you will have to use the initializer list. Same thing with your base class.   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor);  in answer to 3, no its not the sameassuming that mVar is a member varible of the class func;func():mVar(0) {}on construction of mVar its value is set to zerohowever withfunc{  mVar(0);}you firsly construct the var to some value and then assign it 0(via a copy constructor iirc), which is far more overhead than is required (if a copy constructor is used as i belive it is then then compiler might well create a temp object to hold the value, it might be able to avoid it but i wouldnt count on it)Same applies for mVar = 0;(ok, the temp object might not happen for int, float etc but for your own classes its probably going to happen) ;  Thanks for the info! It all makes lot of sense now. ;  For one, why do they use throw() to indicate that a constructor won''t throw? Wouldn''t it be more obvious to use something like nothrow() instead?   --{You fight like a dairy farmer!} ;  Regarding 3. it is sometimes necessary, like when initialising a member reference as it can only be initialised upon creation.class A{public:    Someclass& a;    A(Someclass& s):a(s) {}    // Works    A(Someclass& s) { a = s; } // Doesn''t work}; ;  PS- Initializer lists give you much better performance.And that''s not just me talking, that''s the C++ faq;quote:Original post by Greatwolf   For one, why do they use throw() to indicate that a constructor won't throw? Wouldn't it be more obvious to use something like nothrow() instead?   IIRC, nothrow is only used with new to make it return zero on failure instead of throwing std::bad_alloc:Mom* yourMom = new(nothrow) Mom();  throw() is an empty exception specification, that, as Fruny said, promises not to throw.   [edited by - antareus on February 10, 2004 6:44:08 PM];quote:Original post by Greatwolf  For one, why do they use throw() to indicate that a constructor won''t throw? Wouldn''t it be more obvious to use something like nothrow() instead?   a quick look at the spec (section 15.4) gives the reason for this, throw() indicates the type of exceptions you can expect to be thrown, so somefunc() throw(int, double);   will be expected to only throw exceptions of type int and double, anything else leads to a call to the fucntion ''unexpected()'' (15.5.2)