Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Copy_and_base
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86 #include <iostream> struct Base { char i = 0; //If you comment out private access specifier below, the code won't compile //because Deriv constructor and destructor will implicitly call base class //constructor and destructor. So Base class must have their constructor //and destructor at least protected to grant access from sub-class. //private: Base(){std::cout << "Base default constructor" << std::endl; } virtual ~Base() { std::cout << "Base destructor" << std::endl; } //whilst, Copy constructor and copy assignment does not needs to be accessed //from Derived clases if they were overriden in sub-class. private: Base(const Base&); Base& operator=(const Base&); }; struct Deriv : Base { Deriv(){ std::cout << "Deriv default constructor" << std::endl; } ~Deriv() { std::cout << "Deriv destructor" << std::endl; } //comment out the following copy functions, will generate default implementations //that calls base class copy functions. In case of private copy functions in Base //class, it will be doomed to fail. Deriv(const Deriv&){ std::cout << "Deriv Copy constructor" << std::endl; } Deriv& operator=(const Deriv&){ std::cout <<"Deriv Copy assignment" << std::endl; return *this; } }; int main() { std::cout << "----------" << std::endl; std::cout << "sub-class constructor implicitly calls base constructor" << std::endl; std::cout << "sub-class destructor implicitly calls base destructor" << std::endl; std::cout << "sub-class copy constructor(assignment) also implicitly calls base copy constructor(assignment)" << std::endl; std::cout << " if sub-class did not define its own overridden version of copy constructor(assignment)" << std::endl; std::cout << "----------" << std::endl; Deriv d; std::cout << "----------" << std::endl; Deriv d1 = d; std::cout << "----------" << std::endl; Deriv d2{d1}; std::cout << "----------" << std::endl; d2 = d; std::cout << "----------" << std::endl; }
run
|
edit
|
history
|
help
0
RVO hota hai bhenchod
new
Hangman
vaska
success
defined(FOO)
Redeclare with auto
Memory example
imdying
My Window