在C++编程中,对象创建是开发过程中最基础的操作之一,不同的创建方式对应不同的内存管理策略和使用场景,掌握多种对象创建方式能够帮助开发者更灵活地设计程序结构。

栈上直接创建对象
这是C++中最简单的对象创建方式,对象存储在栈内存中,生命周期由作用域控制,离开作用域会自动调用析构函数释放资源,不需要手动管理内存。
#include <iostream>
using namespace std;
class Person {
public:
Person(string name) : name(name) {
cout << "Person对象创建,姓名:" << name << endl;
}
~Person() {
cout << "Person对象销毁,姓名:" << name << endl;
}
void show() {
cout << "姓名:" << name << endl;
}
private:
string name;
};
int main() {
// 栈上创建对象,调用有参构造函数
Person p1("张三");
p1.show();
// 离开main函数作用域时,p1会自动销毁
return 0;
}
堆上动态创建对象
使用new关键字在堆内存中创建对象,需要手动使用delete释放内存,否则会造成内存泄漏。对象的生命周期由开发者控制,适合需要长期存在或者大小不确定的对象。
#include <iostream>
using namespace std;
class Person {
public:
Person(string name) : name(name) {
cout << "Person对象创建,姓名:" << name << endl;
}
~Person() {
cout << "Person对象销毁,姓名:" << name << endl;
}
void show() {
cout << "姓名:" << name << endl;
}
private:
string name;
};
int main() {
// 堆上创建对象,返回指针
Person* p2 = new Person("李四");
p2->show();
// 手动释放内存
delete p2;
p2 = nullptr;
return 0;
}
使用构造函数创建对象
构造函数是类初始化对象的核心方法,我们可以通过定义不同参数的构造函数,实现不同初始化逻辑的对象创建。如果没有定义构造函数,编译器会生成默认的无参构造函数。
#include <iostream>
using namespace std;
class Rectangle {
public:
// 无参构造函数
Rectangle() : width(0), height(0) {}
// 有参构造函数
Rectangle(int w, int h) : width(w), height(h) {}
// 拷贝构造函数
Rectangle(const Rectangle& other) : width(other.width), height(other.height) {}
int getArea() {
return width * height;
}
private:
int width;
int height;
};
int main() {
Rectangle r1; // 调用无参构造函数
Rectangle r2(3, 4); // 调用有参构造函数
Rectangle r3 = r2; // 调用拷贝构造函数
cout << "r2面积:" << r2.getArea() << endl;
cout << "r3面积:" << r3.getArea() << endl;
return 0;
}
工厂模式创建对象
工厂模式是一种设计模式,通过工厂类来封装对象的创建逻辑,调用者不需要关心对象的具体创建细节,只需要传入对应的参数即可获取对象实例,降低了代码的耦合度。
#include <iostream>
#include <string>
using namespace std;
// 抽象产品类
class Shape {
public:
virtual void draw() = 0;
virtual ~Shape() {}
};
// 具体产品类:圆形
class Circle : public Shape {
public:
void draw() override {
cout << "绘制圆形" << endl;
}
};
// 具体产品类:矩形
class Rectangle : public Shape {
public:
void draw() override {
cout << "绘制矩形" << endl;
}
};
// 工厂类
class ShapeFactory {
public:
static Shape* createShape(const string& type) {
if (type == "circle") {
return new Circle();
} else if (type == "rectangle") {
return new Rectangle();
}
return nullptr;
}
};
int main() {
Shape* shape1 = ShapeFactory::createShape("circle");
shape1->draw();
Shape* shape2 = ShapeFactory::createShape("rectangle");
shape2->draw();
delete shape1;
delete shape2;
return 0;
}
单例模式创建对象
单例模式保证一个类只有一个实例,并且提供全局访问点,适合需要全局唯一对象的场景,比如日志管理器、配置管理器等。需要注意线程安全和内存释放问题。
#include <iostream>
#include <mutex>
using namespace std;
class Singleton {
public:
// 获取单例实例的静态方法
static Singleton* getInstance() {
// 双重检查锁定,保证线程安全
if (instance == nullptr) {
lock_guard<mutex> lock(mtx);
if (instance == nullptr) {
instance = new Singleton();
}
}
return instance;
}
void show() {
cout << "单例对象调用" << endl;
}
// 释放单例对象的方法
static void destroyInstance() {
if (instance != nullptr) {
delete instance;
instance = nullptr;
}
}
private:
// 私有构造函数,防止外部直接创建对象
Singleton() {
cout << "单例对象创建" << endl;
}
// 私有拷贝构造函数,防止拷贝
Singleton(const Singleton&) = delete;
// 私有赋值运算符,防止赋值
Singleton& operator=(const Singleton&) = delete;
static Singleton* instance;
static mutex mtx;
};
// 静态成员初始化
Singleton* Singleton::instance = nullptr;
mutex Singleton::mtx;
int main() {
Singleton* s1 = Singleton::getInstance();
Singleton* s2 = Singleton::getInstance();
s1->show();
// 验证两个指针指向同一个对象
cout << "s1地址:" << s1 << ",s2地址:" << s2 << endl;
Singleton::destroyInstance();
return 0;
}
不同创建方式的对比
不同的对象创建方式有各自的特点,开发者可以根据实际需求选择:
| 创建方式 | 内存区域 | 生命周期控制 | 适用场景 |
|---|---|---|---|
| 栈上创建 | 栈内存 | 作用域自动管理 | 生命周期短、大小确定的临时对象 |
| 堆上创建 | 堆内存 | 手动delete释放 | 需要长期存在、大小不确定的对象 |
| 工厂模式 | 通常堆内存 | 手动或工厂管理释放 | 需要解耦创建逻辑、多种同类产品的场景 |
| 单例模式 | 通常堆内存 | 全局唯一,手动或程序结束释放 | 需要全局唯一实例的场景 |