构造函数、析构函数、等号操作符的理解 C++

本文深入探讨了C++中拷贝构造函数和赋值操作符的实现细节,通过具体示例展示了它们在不同场景下的行为,如对象初始化、赋值和函数参数传递等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

#include<iostream>

using namespace std;


class Test
{
public:

	Test(int x, int y)
	{
		cout << "Test(int x, int y)..." << endl;
		m_x = x;
		m_y = y;
	}
	Test()
	{
		cout << "Test()..." << endl;
		m_x = 0;
		m_y = 0;
	}

	Test(const Test & another)
	{
		cout << "Test(const Test & another)..." << endl;
		m_x = another.m_x + 100;
		m_y = another.m_y+100;
	}
	void operator=(const Test & another)
	{
		cout << "=operator(const Test & another)..." << endl;
		m_x = another.m_x + 100;
		m_y = another.m_y + 100;
	}
	void PrintT()
	{
		cout << "m_x=" << m_x << ",m_y=" << m_y << endl;
	}
	~Test()
	{
		cout << "~Test()..." << endl;
	}


private:
	int m_x;
	int m_y;

};

void test1()
{
	Test t1(10,20);
	Test t2(t1); // Test t1=t2;
}
void test2()
{
	Test t1(10, 20);
	Test t2;
	t2 = t1; //调用=操作符
}


void func1(Test t) //Test t = t1; 调用拷贝构造函数
{
	cout << "fun1() begin..." << endl;
	t.PrintT();
	cout << "fun1()  end ..." << endl;
} //析构t
void test3()
{
	cout << "test3 begin..." << endl;
	Test t1(10, 20);
	func1(t1);
	cout << "test3 end ..." << endl;
}//析构t1


Test func2()
{
	cout << "fun2() begin..." << endl;
	Test temp(10, 20);
	temp.PrintT();
	cout << "fun2()  end ..." << endl;
	return temp; //匿名对象 = temp ; temp 就析构
}
void test4()
{
	cout << "test4 begin..." << endl;
	func2(); //返回来的匿名对象没有被使用,析构匿名对象
	cout << "test4 end ..." << endl;
}


void test5()
{
	cout << "test5 begin..." << endl;
	Test t1 = func2(); //把匿名对象 起名字叫 t1
	cout << "test5 end ..." << endl;
}   //最后析构t1

void test6()
{
	cout << "test6 begin..." << endl;
	Test t1;
	t1 = func2(); //调用=操作符
	cout << "test6 end ..." << endl;
}  




int main()
{
	
	//Test t1(100, 200);
	//Test t2 = t1;
	//Test t3(t2);
	//t1.PrintT();
	//t2.PrintT();
	//t3.PrintT();

//	test1();
//	test2();
//	test3(); 
//	test4();
//	test5();
	test6();
	return 0;
}

 

### C++ String 类的析构函数实现与作用 在 C++ 中,`String` 类的析构函数主要负责释放动态分配的内存资源,确保程序运行时不会出现内存泄漏问题。以下是关于 `String` 类析构函数的实现与作用的详细说明。 #### 1. 析构函数的作用 析构函数的主要功能是在对象生命周期结束时清理资源,例如释放动态分配的内存、关闭文件句柄或网络连接等。对于 `String` 类而言,如果类内部使用了动态分配的内存(如通过 `new[]` 分配的字符数组),析构函数必须显式释放这些内存以避免内存泄漏[^2]。 ```cpp class String { private: char* str; // 动态分配的字符串指针 public: // 构造函数 String(const char* s = nullptr) { if (s == nullptr) { str = new char[1]; str[0] = '\0'; } else { str = new char[strlen(s) + 1]; strcpy(str, s); } } // 拷贝构造函数 String(const String& other) { str = new char[strlen(other.str) + 1]; strcpy(str, other.str); } // 析构函数 ~String() { delete[] str; // 释放动态分配的内存 } // 等号操作符重载 String& operator=(const String& other) { if (this != &other) { // 防止自赋值 delete[] str; // 先释放原有内存 str = new char[strlen(other.str) + 1]; strcpy(str, other.str); } return *this; } }; ``` #### 2. 析构函数的实现细节 - **动态内存管理**:在上述代码中,`str` 是一个指向动态分配内存的指针。当对象被销毁时,析构函数会调用 `delete[] str` 来释放这块内存[^3]。 - **防止内存泄漏**:如果没有定义析构函数,动态分配的内存将无法自动释放,导致内存泄漏问题。 - **深拷贝与浅拷贝**:为了防止多个对象共享同一块内存,拷贝构造函数赋值操作符重载都需要实现深拷贝逻辑。这同样要求析构函数正确释放内存,避免重复释放或悬空指针问题[^1]。 #### 3. 显式调用析构函数 虽然很少需要显式调用析构函数,但在某些特殊情况下(例如对象位于绝对地址或使用用户定义的 `new` 运算符分配内存),可能需要手动调用析构函数以清理资源。以下是一个示例: ```cpp String* pStr = new (std::malloc(sizeof(String))) String("Hello World"); // 使用 malloc 分配内存 pStr->~String(); // 显式调用析构函数清理资源 std::free(pStr); // 手动释放内存 ``` 需要注意的是,显式调用析构函数后,对象的状态变为未定义,不应再访问该对象[^3]。 ### 总结 C++ 中 `String` 类的析构函数主要用于释放动态分配的内存资源,避免内存泄漏问题。它在对象生命周期结束时自动调用,确保程序的安全性稳定性。同时,在特殊情况下可以显式调用析构函数以清理资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值