Trivial classes in C++
When a class or struct in C++ has compiler-provided or explicitly defaulted special member functions, then it is a trivial type. It occupies a contiguous memory area. It can have members with different access specifiers.
Trivial types have a trivial default constructor, trivial copy constructor, trivial copy assignment operator and trivial destructor. In each case, trivial means the constructor/ operator/ destructor is not user-provided and belongs to a class that has :
- No virtual functions or virtual base classes,
- No base classes with a corresponding non-trivial constructor/operator/destructor
- No data members of class type with a corresponding non-trivial constructor/operator/destructor
The following examples show trivial types :
/*Since there are no explicit constructors, there exists a default constructor*/struct Trivial { int i; private: int j; }; /* In Trivial2 structure, the presence of the Trivial2(int a, int b) constructor requires that you provide a default constructor. For the type to qualify as trivial, we must explicitly default that constructor.*/struct Trivial2 { int i; Trivial2(int a, int b) { i = a; } Trivial2() = default; }; |
Reference : https://msdn.microsoft.com/en-us/library/mt767760.aspx
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Virtual functions in derived classes
- Catching base and derived classes as exceptions
- Nested Classes in C++
- Local Classes in C++
- Pure Virtual Functions and Abstract Classes in C++
- C++ Classes and Objects
- Anonymous classes in C++
- File Handling through C++ Classes
- C++ Stream Classes Structure
- C++ program to compare two Strings using Operator Overloading
- not1 and not2 function templates in C++ STL with Examples
- Go vs C++
- Trie Data Structure using smart pointer and OOP in C++
- std::hash class in C++ STL



