找回密码
立即注册
搜索
热搜: Java Python Linux Go
发回帖 发新帖

259

积分

0

好友

37

主题
发表于 前天 14:43 | 查看: 11| 回复: 0

在C++中,拷贝构造函数至关重要,它负责用一个已有的对象来初始化同类型的新对象。编译器并非总会自动生成默认的拷贝构造函数,理解其生成时机是掌握对象复制机制的关键。

当你没有显式定义拷贝构造函数时,编译器在特定情况下才会生成它。例如,若类包含一个类类型成员,且该成员所属的类有拷贝构造函数,编译器就会为当前类生成默认版本。假设Student类包含一个Address类类型的成员,而Address类定义了拷贝构造函数,那么编译器就会为Student生成默认拷贝构造函数。

此外,若类继承自含有拷贝构造函数的基类,编译器也会生成。例如GraduateStudent继承自Student,且Student有拷贝构造函数,那么GraduateStudent在拷贝初始化时就能正确调用基类的拷贝构造函数。同时,当类声明了虚函数或含有虚基类时,编译器同样会生成默认拷贝构造函数,以确保虚函数表指针等关键信息被正确复制,维持多态性的正常运作。要深入理解对象模型与多态性,可以进一步学习算法与数据结构

拷贝构造函数是什么?

拷贝构造函数是一种特殊的构造函数,用于以一个已存在的对象为蓝本,初始化一个新对象。其形参是本类对象的常引用,一般形式为 类名(const 类名& 对象名)

class Student {
public:
    std::string name;
    int age;
    // 拷贝构造函数
    Student(const Student& other) : name(other.name), age(other.age) {
        std::cout << "拷贝构造函数被调用" << std::endl;
    }
};

在上面的代码中,Student(const Student& other) 就是拷贝构造函数。当使用一个已有的 Student 对象初始化另一个新对象时,它就会被调用:

int main() {
    Student s1;
    s1.name = "Alice";
    s1.age = 20;
    Student s2 = s1; // 调用拷贝构造函数
    return 0;
}

Student s2 = s1; 这行代码会调用拷贝构造函数,将 s1 的成员值复制给 s2

拷贝构造函数主要有以下调用场景:

  1. 对象初始化时:使用一个已创建的对象为新对象赋值,如 MyClass obj2 = obj1;
  2. 函数参数传递时:如果函数形参是类对象且采用值传递,调用函数时会创建实参的副本。
    void myFunction(MyClass obj) { // 传递时调用拷贝构造
        // 函数体
    }
  3. 函数返回值时:当函数以值传递方式返回一个类对象,返回时会创建临时对象作为返回值。
    MyClass myFunction() {
        MyClass obj;
        return obj; // 返回时调用拷贝构造
    }

什么时候调用拷贝构造函数

拷贝构造函数主要在以下三种场景被调用:

1. 对象初始化时

使用一个已有对象初始化另一个同类型新对象时。

class Point {
public:
    int x;
    int y;
    Point(int a, int b) : x(a), y(b) {}
    Point(const Point& other) : x(other.x), y(other.y) {
        std::cout << "拷贝构造函数在对象初始化时被调用" << std::endl;
    }
};
int main() {
    Point p1(1, 2);
    Point p2 = p1; // 调用拷贝构造函数
    return 0;
}

Point p2 = p1; 使用 p1 初始化 p2,触发拷贝构造。

2. 函数参数传递时

当对象以值传递方式作为函数参数时。

class Rectangle {
public:
    int width;
    int height;
    Rectangle(int w, int h) : width(w), height(h) {}
    Rectangle(const Rectangle& other) : width(other.width), height(other.height) {
        std::cout << "拷贝构造函数在函数参数传递时被调用" << std::endl;
    }
};
void printRectangle(Rectangle rect) { // 值传递
    std::cout << "Width: " << rect.width << ", Height: " << rect.height << std::endl;
}
int main() {
    Rectangle r1(10, 5);
    printRectangle(r1); // 调用拷贝构造函数,将r1复制给形参rect
    return 0;
}

3. 函数返回值时

当函数以值传递方式返回一个对象时。

class Circle {
public:
    int radius;
    Circle(int r) : radius(r) {}
    Circle(const Circle& other) : radius(other.radius) {
        std::cout << "拷贝构造函数在函数返回值时被调用" << std::endl;
    }
};
Circle createCircle() {
    Circle c(5);
    return c; // 调用拷贝构造函数,创建临时对象返回
}
int main() {
    Circle myCircle = createCircle();
    return 0;
}

createCircle 函数返回时,会调用拷贝构造函数创建临时对象。

四种不适用位拷贝语义的场景

1. 什么是位拷贝语义?

位拷贝语义指通过直接复制对象在内存中的原始字节来实现对象复制,效率高,适用于仅包含基本数据类型成员的简单类。

class SimpleClass {
public:
    int num;
    float f;
};
SimpleClass obj1;
obj1.num = 10;
obj1.f = 3.14f;
SimpleClass obj2 = obj1; // 这里进行的就是位拷贝

2. 场景一:内含需要自定义拷贝行为的成员对象

当一个类包含需要自定义拷贝行为的成员对象(如 std::string)时,位拷贝无法满足需求。

#include <string>
#include <iostream>
class HasString {
public:
    HasString(const std::string& s) : str(s) {
        std::cout << "HasString构造函数被调用" << std::endl;
    }
private:
    std::string str; // std::string有自己的拷贝构造函数(深拷贝)
};
int main() {
    HasString hs1("hello");
    HasString hs2 = hs1; // 不能使用位拷贝,否则hs1.str和hs2.str将指向同一内存
    return 0;
}

实际上,这里会调用 HasString 的默认拷贝构造函数,进而调用 std::string 的拷贝构造函数实现深拷贝。

3. 场景二:继承自具有拷贝构造函数的基类

当派生类继承自一个具有拷贝构造函数的基类时,位拷贝无法正确处理基类部分的复制。

#include <iostream>
class Base {
public:
    Base(int x) : val(x) {
        std::cout << "Base构造函数被调用,val = " << val << std::endl;
    }
    Base(const Base& other) : val(other.val) {
        std::cout << "Base拷贝构造函数被调用,val = " << val << std::endl;
    }
private:
    int val;
};
class Derived : public Base {
public:
    Derived(int x, int y) : Base(x), derivedVal(y) {
        std::cout << "Derived构造函数被调用,derivedVal = " << derivedVal << std::endl;
    }
    // 显式定义拷贝构造函数,正确调用基类拷贝构造
    Derived(const Derived& other) : Base(other), derivedVal(other.derivedVal) {
        std::cout << "Derived拷贝构造函数被调用,derivedVal = " << derivedVal << std::endl;
    }
private:
    int derivedVal;
};

4. 场景三:声明了一个或多个虚函数

虚函数涉及虚函数表(VTable)和虚函数表指针(VPTR)。位拷贝会错误地复制VPTR,破坏多态性。

#include <iostream>
class Base {
public:
    virtual void print() {
        std::cout << "This is Base" << std::endl;
    }
};
class Derived : public Base {
public:
    void print() override {
        std::cout << "This is Derived" << std::endl;
    }
};
int main() {
    Derived d;
    Base b = d; // 如果位拷贝,b的VPTR指向Base的虚表
    b.print(); // 将调用Base::print(),而非Derived::print()
    return 0;
}

5. 场景四:继承链中存在一个或多个虚基类

虚基类用于解决菱形继承问题,确保在最终派生类中只保留一份基类实例。位拷贝无法正确处理虚基类表的拷贝。

class A {
public:
    int data;
};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};
int main() {
    D d1;
    d1.data = 10;
    D d2 = d1; // 位拷贝会导致虚基类A部分初始化错误
    return 0;
}

虚基类的初始化由最终派生类(D)的构造函数负责,位拷贝会破坏这一机制。

编译器什么时候生成默认拷贝构造函数

编译器在以下必要情况下会生成默认拷贝构造函数:

1. 类中含有成员对象

当类包含其他类类型的成员对象,且这些成员有自己的拷贝构造函数时。

class Point {
public:
    int x; int y;
    Point(int a, int b) : x(a), y(b) {}
    Point(const Point& other) : x(other.x), y(other.y) {
        std::cout << "Point类的拷贝构造函数被调用" << std::endl;
    }
};
class Rectangle {
public:
    Point topLeft;
    Point bottomRight;
    Rectangle(int x1, int y1, int x2, int y2) : topLeft(x1, y1), bottomRight(x2, y2) {}
    // 未定义拷贝构造函数,编译器会生成默认的
};
int main() {
    Rectangle r1(1, 1, 5, 5);
    Rectangle r2 = r1; // 编译器生成默认拷贝构造函数,并调用Point的拷贝构造
    return 0;
}

2. 存在继承关系

当一个类继承自有拷贝构造函数的基类时。

class Animal {
public:
    std::string name;
    Animal(const std::string& n) : name(n) {}
    Animal(const Animal& other) : name(other.name) {
        std::cout << "Animal类的拷贝构造函数被调用" << std::endl;
    }
};
class Dog : public Animal {
public:
    int age;
    Dog(const std::string& n, int a) : Animal(n), age(a) {}
    // 未定义拷贝构造函数,编译器会生成
};
int main() {
    Dog d1("Buddy", 3);
    Dog d2 = d1; // 编译器生成默认拷贝构造函数,先调用Animal的拷贝构造
    return 0;
}

3. 类中有虚函数

类中含有虚函数时,需要正确处理虚函数表指针的拷贝。

class Shape {
public:
    virtual void draw() const = 0;
    int color;
    Shape(int c) : color(c) {}
    // 未定义拷贝构造函数,编译器会生成
};
class Circle : public Shape {
public:
    int radius;
    Circle(int c, int r) : Shape(c), radius(r) {}
    void draw() const override {
        std::cout << "Drawing a circle" << std::endl;
    }
    // 未定义拷贝构造函数,编译器会生成
};
int main() {
    Circle c1(255, 5);
    Circle c2 = c1; // 编译器生成默认拷贝构造函数,处理虚表指针
    return 0;
}

4. 虚继承情况

当一个类使用虚继承时,需要处理虚基类表指针的拷贝。

class Base { public: int baseData; Base(int d) : baseData(d) {} };
class Derived1 : virtual public Base { public: Derived1(int d) : Base(d) {} };
class Derived2 : virtual public Base { public: Derived2(int d) : Base(d) {} };
class Final : public Derived1, public Derived2 {
public:
    int finalData;
    Final(int b, int f) : Base(b), Derived1(b), Derived2(b), finalData(f) {}
    // 未定义拷贝构造函数,编译器会生成
};
int main() {
    Final f1(10, 20);
    Final f2 = f1; // 编译器生成默认拷贝构造函数,处理虚基类表
    return 0;
}

构造函数误区深度剖析

误区一:默认拷贝构造函数适用于所有情况

默认拷贝构造函数执行浅拷贝。当类包含指针成员时,它只复制指针地址,不复制指针指向的内存,导致多个对象共享同一资源,引发重复释放、野指针和数据不一致等问题。

class MyString {
private:
    char* str;
public:
    MyString(const char* s) {
        str = new char[strlen(s) + 1];
        strcpy(str, s);
    }
    ~MyString() {
        delete[] str;
    }
    // 没有定义拷贝构造函数 -> 使用默认浅拷贝
};
int main() {
    MyString s1("Hello");
    MyString s2 = s1; // 灾难!s2.str 和 s1.str 指向同一内存
    return 0; // 析构时同一内存被释放两次,程序崩溃
}

此时必须自定义拷贝构造函数实现深拷贝

误区二:只要不手动调用就不会执行

拷贝构造函数的调用常由编译器隐式完成,容易被忽略。

  • 对象初始化Point p2(p1);Point p2 = p1;
  • 函数参数值传递void func(Point p) {...} 调用 func(p1) 时。
  • 函数返回值Point func() { Point tmp; return tmp; }

误区三:和赋值运算符重载混淆

两者功能和调用时机不同:

  • 拷贝构造函数:创建新对象时,用于初始化
  • 赋值运算符重载 (operator=):两个已存在对象间进行赋值
    class Number {
    public:
    int value;
    Number(int v) : value(v) {}
    // 拷贝构造函数
    Number(const Number& other) : value(other.value) {
        std::cout << "拷贝构造函数被调用" << std::endl;
    }
    // 赋值运算符重载
    Number& operator=(const Number& other) {
        if (this != &other) {
            value = other.value;
            std::cout << "赋值运算符重载函数被调用" << std::endl;
        }
        return *this;
    }
    };
    int main() {
    Number n1(10);
    Number n2(n1); // 调用拷贝构造函数(初始化新对象n2)
    Number n3(20);
    n3 = n1; // 调用赋值运算符重载(对已存在的n3赋值)
    return 0;
    }

默认拷贝构造函数详解

1. 默认拷贝构造函数是什么?

当用户没有为类显式定义拷贝构造函数时,编译器在必要时生成的版本。它执行成员级别的复制:对基本类型直接复制值;对类类型成员调用其拷贝构造函数。

2. 默认拷贝构造函数何时生成?

  • 用户未自定义拷贝构造函数,且代码中出现了需要拷贝构造的场景(对象初始化、函数传参、函数返回)。
  • 用户定义了其他构造函数(非拷贝构造),但只要没定义拷贝构造,且需要拷贝时,编译器依然会生成。

3. 默认拷贝构造函数工作原理

  • 内存层面:执行浅拷贝,按照对象成员在内存中的顺序逐字节复制。
  • 类型处理差异
    • 内置类型:直接进行值拷贝。
    • 自定义类型:调用该类型的拷贝构造函数完成拷贝。

4. 什么时候需要自定义拷贝构造函数?

当类涉及资源管理(尤其是动态内存分配)时,必须自定义深拷贝构造函数以避免浅拷贝问题。 浅拷贝的问题(以管理字符串的类为例):

  • 内存重复释放。
  • 野指针。
  • 数据一致性被破坏。

深拷贝的实现

class String {
public:
    char* str;
    int length;
    String(const char* s) {
        length = strlen(s);
        str = new char[length + 1];
        strcpy(str, s);
    }
    // 深拷贝构造函数
    String(const String& other) {
        length = other.length;
        str = new char[length + 1]; // 为新对象独立分配内存
        strcpy(str, other.str); // 复制内容
    }
    ~String() {
        delete[] str;
    }
};

深拷贝构造函数为新对象分配独立的内存并复制内容,从根本上解决了共享资源带来的所有问题,是编写健壮C++代码的关键。对于复杂的数据结构和资源管理,可以参考数据库与中间件中关于数据持久化和高效存取的设计思想。

您需要登录后才可以回帖 登录 | 立即注册

手机版|小黑屋|网站地图|云栈社区(YunPan.Plus) ( 苏ICP备2022046150号-2 )

GMT+8, 2025-12-3 14:50 , Processed in 0.071887 second(s), 38 queries , Gzip On.

Powered by Discuz! X3.5

© 2025-2025 CloudStack.

快速回复 返回顶部 返回列表