c++ - Create an object that contains another object -


i studying c++, , bit confused following situation:

for example,

class apple

  class apple   {     int kg;     apple();   } 

class fruit

  class fruit   {     private:       int count;       apple one;      public:       fruit();//do need call constructor apple,               //or default fruit constructor calls well?   }    

in example default constructor gets called. if apple constructor example took int first param have set through initializer list:

class apple { public:     int kg;     apple(int _kg) :          kg(_kg)     {} };  class fruit { private:     int count;     apple one;  public:     fruit() : one(5) //right here or you'll error     {        this->count = 5;        //this->one(5) doesn't work.     } };  

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -