Think about some basic concepts (continuously updating)

Source: Internet
Author: User

I. Why cannot constructors be virtual functions:

1. The so-called virtual function determines which function to call based on the dynamic type of the object.
2. constructor runs before the object is fully constructed. In other words, before the constructor is run, the object has not yet been generated, let alone the dynamic type.
In this case, the constructor cannot be a virtual function.


Ii. function templates

A function template is a function that does not describe the data type of certain parameters. For example, the following defines a function template that can perform operations (absolute values) on any type of variables:

Template <class T> // or written as: template <typename T> T abs (T val) {return val <0? -Val: val ;}

When a function template is called, the compiler determines the type of the template parameter T based on the actual parameter type, and automatically generates a corresponding function, that is, the template function. Different template parameters have different types, and different template functions are generated.

3. Controls occupied by instantiated objects of a class

What is the space occupied by the instantiated object of a class? Note that the class size is the object size of the class.

First, what is the class size? Specifically, a class is just a type definition and there is no size.Use the sizeof operator to operate on a type name.(Object)Size.
If Class A; A obj; then sizeof (A) = sizeof (obj ).

The size of an object is greater than or equal to the total size of all non-static members.

Why is it equal to or greater than just equal? The excess has the following two main aspects: 1) the C ++ object model itself requires a method to provide type information (RTTI) for objects with virtual functions) and virtual function entry. A common method is to create a virtual function entry table, which can be shared with objects of the same type. Therefore, an object must have a pointer to the virtual function table, to support RTTI, many compilers place this type of information in the virtual function table. However, whether this implementation method must be used is not specified by the C ++ standard, but these are a solution adopted by mainstream compilers. 2) compiler optimization
For most CPUs, the integer double of the CPU length is faster, so if these members are not enough to add up to this integer multiple, the compiler may insert additional content to add up to this integer multiple, in addition, sometimes adjacent members may be inserted with spaces for this purpose, which is called "padding ). Therefore, the C ++ standard strictly stipulates that the Members are arranged in the class-defined order, but they are not required to be closely arranged in the memory. Based on the above two points, we can say that we use sizeof to operate on the class name. The result is the size of the bytes occupied by the class object in the memory. Because the static member variables are not stored in the object, therefore, this result is equal to the sum of all non-static data members (excluding member functions) plus the additional bytes added by the compiler.

Sample Code:

class A{public:    static int i;};int A::i = 0; int main(){    cout << sizeof(A) << endl;//1    return 1;}

The latter depends on different compiler implementations, and the c ++ standard does not guarantee this. The C ++ Standard specifies that the class size is not 0, and the empty class size is 1. When the class does not contain virtual functions and non-static data members, the object size is also 1.
If a virtual function (one or more) is declared in the class, the compiler automatically inserts a pointer to the virtual function table in the object during Object Instantiation, on a 32-bit machine, an object will add 4 bytes to store the pointer, which is the key to achieving polymorphism in the object orientation. Like other member functions, virtual functions do not occupy the space of objects.
Let's take a look at the following example: (this example is compiled and run in the Visual C ++ compiler)

Sample Code

Class A {}; Class B {char ch; void func () {}}; class c {char character; // occupies the byte char CH2; // occupies the byte virtual void func () {}}; Class D {int in; virtual void func () {}}; void main () {A; B; c; d; cout <sizeof (a) <Endl; // result = 1 cout <sizeof (B) <Endl; // result = 1 // Object C actually only contains bytes of useful data, but // according to the compiler optimization above, the compiler extends // to two characters, namely, bytes, there is a virtual table address, which is an integer multiple of its type, cout <sizeof (c) <Endl; // result = 8 cout <sizeof (d) <Endl; // result = 8}

To sum up:

In a class, virtual functions, member functions (including static and non-static), and static data members do not occupy the storage space of class objects.

Object size = sizeof (vptr) + size of all non-static data members +
The size of aligin bytes (depending on different compilers ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.