构造器

  • 构造器和通常方法主要区别:
    • 构造器的名字必须和它所在的类名字一样
    • 系统在创建某个类的实例时会在第一时间调用这个类构造器
    • 系统在创建某个类的实例时会在第一时间自动调用这个类构造器
    • 构造器永远不会返回任何值
  • 创建构造器,需要把它的声明添加到类里

    1. class Car{
    2. Car(void);
    3. }
  • 注意大小写和类名保持一致,结束声明后开始定义构造器本身

    1. Car::Car(void){
    2. color="white";
    3. engine="v8";
    4. wheel=4;
    5. gas_tank=FULL_GAS;
    6. }
  • 每个类至少有一个构造器 ,如果在类里没有定义,编译器会自己给你定义一个:ClassName::ClassName(){}

  • 除此之外,编译器还会创建一个副本构造器CopyConstructor ```cpp

    include

    include

    include

struct Sclass { int a; int b; int c; int d; Sclass() { printf(“look this function”); } int Plus() { return a + b + c + d; } }; int main(int argc, char* argv[]) {

  1. Sclass s;
  2. return 0;

}

  1. ```cpp
  2. Sclass s;
  3. 008543A2 lea ecx,[s]
  4. 008543A5 call Sclass::Sclass (08513B1h)
  5. return 0;
  6. struct Sclass
  7. {
  8. int a;
  9. int b;
  10. int c;
  11. int d;
  12. Sclass() {
  13. 00851740 push ebp
  14. 00851741 mov ebp,esp
  15. 00851743 sub esp,0CCh
  16. 00851749 push ebx
  17. 0085174A push esi
  18. 0085174B push edi
  19. 0085174C push ecx
  20. 0085174D lea edi,[ebp-0CCh]
  21. 00851753 mov ecx,33h
  22. 00851758 mov eax,0CCCCCCCCh
  23. 0085175D rep stos dword ptr es:[edi]
  24. 0085175F pop ecx
  25. 00851760 mov dword ptr [this],ecx
  26. 00851763 mov ecx,offset _A192CE94_源@cpp (085C015h)
  27. 00851768 call @__CheckForDebuggerJustMyCode@4 (085130Ch)
  28. printf("look this function");
  29. 0085176D push offset string "look this function" (0857BCCh)
  30. 00851772 call _printf (08513A2h)
  31. 00851777 add esp,4
  32. }
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

struct Sclass
{
    int a;
    int b;
    int c;
    int d;
    Sclass(int a,int b,int c,int d) {
        this->a = a;
        this->b = b;
        this->c = c;
        this->d = d;
        printf("look this function");
    }
    int Plus() {
        return a + b + c + d;
    }
};
int main(int argc, char* argv[]) {

    Sclass s(1,2,3,4);
    return 0;
}
Sclass s(1,2,3,4);
00F243A2  push        4  
00F243A4  push        3  
00F243A6  push        2  
00F243A8  push        1  
00F243AA  lea         ecx,[s]  
00F243AD  call        Sclass::Sclass (0F213BBh)  
    return 0;

struct Sclass
{
    int a;
    int b;
    int c;
    int d;
    Sclass(int a,int b,int c,int d) {
00F22600  push        ebp  
00F22601  mov         ebp,esp  
00F22603  sub         esp,0CCh  
00F22609  push        ebx  
00F2260A  push        esi  
00F2260B  push        edi  
00F2260C  push        ecx  
00F2260D  lea         edi,[ebp-0CCh]  
00F22613  mov         ecx,33h  
00F22618  mov         eax,0CCCCCCCCh  
00F2261D  rep stos    dword ptr es:[edi]  
00F2261F  pop         ecx  
00F22620  mov         dword ptr [this],ecx  
00F22623  mov         ecx,offset _A192CE94_源@cpp (0F2C015h)  
00F22628  call        @__CheckForDebuggerJustMyCode@4 (0F2130Ch)  
        this->a = a;
00F2262D  mov         eax,dword ptr [this]  
00F22630  mov         ecx,dword ptr [a]  
00F22633  mov         dword ptr [eax],ecx  
        this->b = b;
00F22635  mov         eax,dword ptr [this]  
00F22638  mov         ecx,dword ptr [b]  
00F2263B  mov         dword ptr [eax+4],ecx  
        this->c = c;
00F2263E  mov         eax,dword ptr [this]  
00F22641  mov         ecx,dword ptr [c]  
00F22644  mov         dword ptr [eax+8],ecx  
        this->d = d;
00F22647  mov         eax,dword ptr [this]  
00F2264A  mov         ecx,dword ptr [d]  
00F2264D  mov         dword ptr [eax+0Ch],ecx  
        printf("look this function");
00F22650  push        offset string "look this function" (0F27BCCh)  
00F22655  call        _printf (0F213A2h)  
00F2265A  add         esp,4  
    }
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

struct Sclass
{
    int a;
    int b;
    int c;
    int d;
    Sclass() {
        printf("无参构造函数");
    }
    Sclass(int a,int b,int c,int d) {
        this->a = a;
        this->b = b;
        this->c = c;
        this->d = d;
        printf("look this function");
    }
    int Plus() {
        return a + b + c + d;
    }
};
int main(int argc, char* argv[]) {

    Sclass s(1,2,3,4);
    return 0;
}
  1. 与类同名没有返回值
  2. 创建对象时候执行、初始化
  3. 可以有多个(最好有一个无参的),称为重载,其他函数也可以重载
  4. 用于清理
  5. 编译器不要求必须提供

    解构器

  • 何时调用解构器要看对象在哪里创建:如果是在栈中创建,函数执行完即解构;如果在堆中创建 手动free时候调用

  • 对象在创建时会自动调用一个特殊方法,即构造器

  • 相应的销毁对象时也会要调用特殊方法 :解构器
  • 构造器用来申请内存,解构器用来清理内存
  • 解构器不返回任何值不带参数 。解构器格式如下: ~ClassName();
  • 解构器可有可无,但是复杂类里很重要,需要用解构器释放内存,不然会造成内存泄露

析构函数可以用于释放堆内存,关闭连接 ,关闭文件

栈中创建

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

struct Person
{
    Person() {
        printf("create object");
    }
    ~Person() {
        printf("destory object");
    }

};
int main(int argc, char* argv[]) {

    Person p;
    return 0;
}
    Person p;
00391812  lea         ecx,[p]  
00391815  call        Person::Person (03913C5h)  //开始调用 
    return 0;
0039181A  mov         dword ptr [ebp-0D8h],0  
00391824  lea         ecx,[p]  
00391827  call        Person::~Person (03913C0h)   // 在return 0 之前调用
0039182C  mov         eax,dword ptr [ebp-0D8h]  
}

{
    Person() {
00392600  push        ebp  
00392601  mov         ebp,esp  
00392603  sub         esp,0CCh  
00392609  push        ebx  
0039260A  push        esi  
0039260B  push        edi  
0039260C  push        ecx  
0039260D  lea         edi,[ebp-0CCh]  
00392613  mov         ecx,33h  
00392618  mov         eax,0CCCCCCCCh  
0039261D  rep stos    dword ptr es:[edi]  
0039261F  pop         ecx  
00392620  mov         dword ptr [this],ecx  
00392623  mov         ecx,offset _A192CE94_源@cpp (039C015h)  
00392628  call        @__CheckForDebuggerJustMyCode@4 (039130Ch)  
        printf("create object");
0039262D  push        offset string "look this function" (0397BCCh)  
00392632  call        _printf (03913A2h)  
00392637  add         esp,4  
    }


     ~Person() {
00393360  push        ebp  
00393361  mov         ebp,esp  
00393363  sub         esp,0CCh  
00393369  push        ebx  
0039336A  push        esi  
0039336B  push        edi  
0039336C  push        ecx  
0039336D  lea         edi,[ebp-0CCh]  
00393373  mov         ecx,33h  
00393378  mov         eax,0CCCCCCCCh  
0039337D  rep stos    dword ptr es:[edi]  
0039337F  pop         ecx  
00393380  mov         dword ptr [this],ecx  
00393383  mov         ecx,offset _A192CE94_源@cpp (039C015h)  
00393388  call        @__CheckForDebuggerJustMyCode@4 (039130Ch)  
        printf("destory object");
0039338D  push        offset string "destory object" (0397BDCh)  
00393392  call        _printf (03913A2h)  
00393397  add         esp,4  
    }