Default Member-Functions Created by the Compiler Inside the C++

When we declare a class in C++

class Declare {};         

The compiler then synthesis class as

class Declare {
public:
    Declare();                        // default constructor
    Declare(const Declare&);            // copy c'tor
    Declare& operator=(Declare Thing&); // copy-assign
    ~Declare();                       // d'tor
    // C++11:
    Declare(Declare&&);                 // move c'tor
    Declare& operator=(Declare&&);      // move-assign
};          

  • We can say that following 6 thing compiler generate by default

  1. default constructor
  2. copy constructor
  3. copy-assign operator
  4. destructor
  5. move constructor
  6. move-assign operator

But this is true till C++14

  • The compiler creates all default/implicitly-declared functions when its needed. A compiler cannot create default member function when it's not in use.

Way Class/Object Oriented Code Transform Into Sequential Code.

class Compiler_treat
{
    int m_var;

public:
    void print()
    {
        cout << m_var << endl;
    }
};        

The compiler will treat it as

class Compiler_treat
{
    int m_var;
};

inline void print()
? {
? ? std::cout.operator<<(this->m_var).operator<<(std::endl);
? }
??
};        
No alt text provided for this image

As you can see above object and methods are separate entity. An object only represent data members.

Therefore all the method in class/struct contain implicit this pointer as the first argument using which all non-static data member can be accessed.

Static data member are not part of class/struct. Because it usually resides in a data segment of memory layout. So it access directly or using segment register.

So this is the reason if you print the size of the above class , it print 4 because all method are a separate entity which operates on object by using implicit this pointer.

By the way

There are many factors that decide the size of an object of a class in C++.

These factors are:

  • Size of all non-static data members
  • Order of data members
  • Byte alignment or byte padding
  • Size of its immediate base class
  • The existence of virtual function(s) (Dynamic polymorphism using virtual functions).
  • Compiler being used

How & Where Constructor Code Transform/Synthesize With Inheritance & Composition Class?


Mode of inheritance (virtclass Foo 
{ 
public: 
  Foo(){cout<<"Foo"<<endl;} 
  ~Foo(){cout<<"~Foo"<<endl;} 
};

class base 
{ 
public: 
  base(){cout<<"base"<<endl;}
  ~base(){cout<<"~base"<<endl;}
};

class Bar /* : public base */
{ 
  Foo foo; 
  char *str; 
public: 
  Bar()
  {
    cout<<"Bar"<<endl;
    str = 0;
  }
  ~Bar(){cout<<"~Bar"<<endl;}
};        


No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

  • Similarly, multiple class member objects require a constructor initialization. The language specifies that the constructors would be invoked in the order of member declaration within the class. This is accomplished by the compiler.
  • But, if an object member does not define a default constructor, a non-trivial default constructor synthesizes by a compiler for respective classes.
  • Moreover, in the case of inheritance, the constructor calling sequence starts from base(top-down) to derived manner. Constructor synthesis & augmentation remain same as above.
  • So in the above case, if you derive?Bar?from?Base?then constructor calling sequence would be?Base?->?Foo?->?Bar.

Bar::Bar()
{
  foo.Foo::Foo(); // augmented compiler code
  
  cout<<"Bar"<<endl; // explicit user code
  str = 0; // explicit user code
}        


How & Where Destructor Code Transform/Synthesize With Inheritance & Composition Class?

  • In case of the destructor, calling sequence is exactly the reverse that of a constructor. Like in the above case it would be?Bar?->?Foo?->?Base. Synthesis & augmentation remain same as above. Access and all other things remain the same.

要查看或添加评论,请登录

abhinav Ashok kumar的更多文章

  • Parallel programming Evolution

    Parallel programming Evolution

    Parallel programming has revolutionized how we leverage modern computing power! From instruction-level parallelism…

  • LLVM vs. GCC: A Comprehensive Comparison

    LLVM vs. GCC: A Comprehensive Comparison

    When it comes to compiling C, C++, and other languages, LLVM and GCC are two of the most widely used compiler…

  • Exploring TVM for Beginners: A Must-Read Guide for Compiler Enthusiasts

    Exploring TVM for Beginners: A Must-Read Guide for Compiler Enthusiasts

    For those diving into machine learning compilers, TVM is a powerful tool that optimizes deep learning models for…

  • Optimizing LLVM Passes: Understanding Pass Execution Time

    Optimizing LLVM Passes: Understanding Pass Execution Time

    Optimizing LLVM passes is crucial for improving performance and efficiency for compiler engineers. A key aspect of this…

  • CPP MCQ Stack

    CPP MCQ Stack

    Welcome to Compiler Sutra — the place to be if you want to improve at C++ and compilers! Link :…

    1 条评论
  • Disabling LLVM Pass

    Disabling LLVM Pass

    ?? Disabling an LLVM Pass for Custom Compiler Modifications ?? LLVM is at the core of many modern compilers, and its…

    1 条评论
  • How LLVM Solve Traditional Compiler Problem m*n

    How LLVM Solve Traditional Compiler Problem m*n

    LLVM (Low-Level Virtual Machine) is a compiler framework that helps compiler developers to transform and build…

  • Pass In LLVM To Count the Number of Instructions in It

    Pass In LLVM To Count the Number of Instructions in It

    You can read the full tutorial here: Read the Full Tutorial This tutorial explores FunctionCount.cpp, a practical…

  • Unlocking C++11 part 2

    Unlocking C++11 part 2

    Hello, Tech Enthusiasts Here is the link for the Unlocking C++11 Part 1 The C++11 standard has transformed how we write…

    1 条评论
  • Unlocking C++11

    Unlocking C++11

    Hello, Tech Enthusiasts! The C++11 standard has transformed how we write C++ code by introducing new features to make…

社区洞察

其他会员也浏览了