You can not initialize one class data member in another class if base class does not have default constructor
class base{
int _x;
public:
base(){}
base(int x): _x{x} {}
};
class derive{
base b; //we are here calling default constructor of base as no //parameter passed
public:
derived(base x) {b = x;} //called first, copy constructor called from here
};
You can not initialize base class data member from child class without initializer list
class base{
int _x;
public:
base(int x): _x{x} {}
}
class derived : public base{
int _y;
public:
derived(int x,int y): base{x}, _y{y} {}
//base{x} calls constructor of base class
/* this is not gonna work, _x is private here
so you must use intializer list
derived(int x, int y){
_x = x;
_y = y;
}
*/
};
int main(){
derived d(4,5);
return 0;
}
You have temp variable same as data member
You can not initialize data member with same name as temporary variable without initializer list
class base{
int _x;
public:
base(int _x): _x{_x} {}
//base(int _x) { _x = _x; } there is errorneous
};
Using initializer list optimize you code a little more
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Arguably the single most important piece of documentation for any open source project is the README. A good README not only informs people what the project does and who it is for but also how they use and contribute to it.
If you write a README without sufficient explanation of what your project does or how people can use it then it pretty much defeats the purpose of being open source as other developers are less likely to engage with or contribute towards it.
Top comments (0)