C stacndard only defines min size for primitive integer data types.
2 <= sizeof(short) <= sizeof(int) <= sizeof(long) >= 4
reference: Data structure alignment
The CPU performs reads and writes to memory most efficiently when the data is naturally aligned. To ensure natural alignment, it may be necessary to insert some padding between structure elements or after the last element of a structure. For example, on a 32-bit machine, a data structure containing a 16-bit value followed by a 32-bit value could have 16 bits of padding between the 16-bit value and the 32-bit value to align the 32-bit value on a 32-bit boundary.
Here is a structure with members of various types, totaling 8 bytes before compilation:
After compilation the data structure will be supplemented with padding bytes to ensure a proper alignment for each of its members:
declare same size together, small to larger (MixedData2) to optimize size of MixedData
reference(Mandarin): https://magicjackting.pixnet.net/blog/post/221968938
That’s why there must be assigned an initial value while reference declared.
In the call by value method, a copy of the parameter is passed to the functions. For these copied values a new memory is assigned and changes made to these values do not reflect the variable in the caller. In call by reference method, we pass the address of the variable and the address is used to access the actual argument used in the function call. So changes made in the parameter alter the passing argument.
post-increment ‘itr++’ operator is more expensive than the pre-increment ‘itr’ operator. The post-increment operator generates a copy of the element before proceeding with incrementing the element and returning the copy. Moreover, most compilers will automatically optimize i by converting it implicitly into ++i
Answer: default access level different. struct public members by default, whlie class private members by default
Placement new operator: a special version of operator new that allocates storage from a preallocated, large block of memory (this version already in #include<new>)
New operator syntax with placement new
As we can see, we can specify an address where we want a new object of given type to be constructed.
The two types of polymorphism in c++ are:
A feature of the C++ programming language (template metaprogramming) that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.
In computer programming, operator overloadingis a specific case of polymorphism in which some or all of operators like +, =, or == have different implementations depending on the types of their arguments.
a programming language construct designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution. In general, an exception is handled (resolved) by saving the current state of execution in a predefined place and switching the execution to a specific subroutine known as an exception handler. Depending on the situation, the handler may later resume the execution at the original location using the saved information. For example, a page fault will usually allow the program to be resumed, while a division by zero might not be resolvable transparently.
reference: https://stackoverflow.com/questions/25163105/stdexcept-vs-exception-headers-in-c
[Effective C++ #7]: Declare destructors virtual in polymorphic base classes.
not declare any virtual member function if a class is not designed to be inherited - vtpr overhead (additional size of one pointer: 32-bit or 64-bit depends on machine)
Default constructor, copy constructor, assignment operator ,destructor ,address-of operator if a class does not explicitly declare these member functions, compiler will:
Initial is necessary. Otherwise, the compiler is not able to know the type.
for logical constantness [Effective C++]
甚麼是《物件導向設計》?
《物件導向設計》最重要的精神是模組化(Modularity)與簡潔介面必須被封裝(Encapsulation) 抽象(Abstraction)資料型別保證只能被適當的方法操作 類別間的繼承(Inheritance)組合(Composition),把曾經做過的東西加以設定並組合,以減輕工作量,同時減少錯誤發生的機會
封裝 encapsulation Preventing unauthorized access to some piece of information or functionality. The key money-saving insight is to separate the volatile part of some chunk of software from the stable part.
抽象 abstraction The mechanism and practice of abstraction reduces and factors out details so that one can focus on a few concepts at a time. (abstraction - a concept or idea not associated with any specific instance)
繼承 inheritance Inheritance is a way to compartmentalize and reuse code by creating collections of attributes and behaviors called objects which can be based on previously created objects.
多型 polymorphism A programming language feature that allows values of different data types to be handled using a uniform interface.
private constructor or private destructor
If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. The standard way to prevent this is to enclose the entire real contents of the file in a conditional, like this:
reference : https://gcc.gnu.org/onlinedocs/cpp/Once-Only-Headers.html
Answer: non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).