C++高级函数调用全解析
立即解锁
发布时间: 2025-08-17 01:59:01 阅读量: 1 订阅数: 3 

### C++高级函数调用全解析
#### 1. 重载成员函数
在C++中,函数重载是一种强大的特性,成员函数也可以进行重载。这意味着可以定义多个同名但参数不同的成员函数。例如,在`Rectangle`类中,有两个`drawShape()`函数:
```cpp
#include <iostream>
class Rectangle
{
public:
Rectangle(int width, int height);
~Rectangle() {}
void drawShape() const;
void drawShape(int width, int height) const;
private:
int width;
int height;
};
Rectangle::Rectangle(int newWidth, int newHeight)
{
width = newWidth;
height = newHeight;
}
void Rectangle::drawShape() const
{
drawShape(width, height);
}
void Rectangle::drawShape(int width, int height) const
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
std::cout << "*";
std::cout << std::endl;
}
}
int main()
{
Rectangle box(30, 5);
std::cout << "drawShape():" << std::endl;
box.drawShape();
std::cout << "\ndrawShape(40, 2):" << std::endl;
box.drawShape(40, 2);
return 0;
}
```
这个程序展示了如何重载`drawShape()`函数。第一个`drawShape()`函数不接受参数,它调用第二个`drawShape()`函数并传入当前对象的宽度和高度。第二个`drawShape()`函数接受两个参数,用于绘制指定大小的矩形。
编译器根据传入的参数数量和类型来决定调用哪个版本的函数。例如,在`main`函数中,第一次调用`box.drawShape()`时,编译器会调用无参数的版本;第二次调用`box.drawShape(40, 2)`时,会调用接受两个参数的版本。
#### 2. 使用默认值
和普通函数一样,类的成员函数也可以有默认值。例如,在`Rectangle2`类中,`drawShape()`函数有一个默认参数:
```cpp
#include <iostream>
class Rectangle
{
public:
Rectangle(int width, int height);
~Rectangle(){}
void drawShape(int aWidth, int aHeight, bool useCurrentValue = false) const;
private:
int width;
int height;
};
Rectangle::Rectangle(int aWidth, int aHeight)
{
width = aWidth;
height = aHeight;
}
void Rectangle::drawShape(int aWidth, int aHeight, bool useCurrentValue) const
{
int printWidth;
int printHeight;
if (useCurrentValue == true)
{
printWidth = width;
printHeight = height;
}
else
{
printWidth = aWidth;
printHeight = aHeight;
}
for (int i = 0; i < printHeight; i++)
{
for (int j = 0; j < printWidth; j++)
std::cout << "*";
std::cout << std::endl;
}
}
int main()
{
Rectangle box(20, 5);
std::cout << "drawShape(0, 0, true) ..." << std::endl;
box.drawShape(0, 0, true);
std::cout <<"drawShape(25, 4) ..." << std::endl;
box.drawShape(25, 4);
return 0;
}
```
在这个例子中,`useCurrentValue`参数的默认值为`false`。如果调用时不提供该参数,它将使用默认值。如果`useCurrentValue`为`true`,则使用对象的当前宽度和高度;否则,使用传入的参数。
使用默认值可以减少函数重载的数量,但当需要更多变化时,重载函数可能更易于扩展和维护。例如,如果用户只提供宽度或高度,使用重载函数更容易实现。
#### 3. 对象初始化
构造函数也可以重载,这为对象的创建提供了更大的灵活性。例如,一个矩形对象可以有两个构造函数:一个接受长度和宽度作为参数,创建指定大小的矩形;另一个不接受参数,创建默认大小的矩形。
```mermaid
graph TD;
A[创建对象] --> B{参数数量和类型};
B -->|有长度和宽度参数| C[调用带参数构造函数];
B -->|无参数| D[调用默认构造函数];
```
构造函数的创建分为两个阶段:初始化阶段和构造函数体。成员变量可以在初始化阶段设置,也可以在构造函数体中赋值。例如:
```cpp
Tricycle::Tricycle():
speed(5),
wheelSize(12)
{
// 构造函数体
}
```
在这个例子中,`speed`和`wheelSize`在初始化阶段被设置为5和12。
需要注意的是,引用和常量必须在初始化阶段设置,因为它们不能被赋值。
#### 4. 复制构造函数
编译器会提供一个默认的复制构造函数,当创建对象的副本时会调用它。默认的复制构造函数进行浅拷贝,即简单地复制每个成员变量的值。对于大多数成员变量来说,浅拷贝是可行的,但对于指向堆上对象的指针成员变量,浅拷贝会导致问题。
例如,在`Tricycle`类中,如果有一个指向整数的指针成员变量`speed`,使用默认复制构造函数会使两个对象的`speed`指针指向同一块内存。当其中一个对象销毁时,会释放这块内存,另一个对象的指针将成为悬空指针,访问它会导致程序崩溃。
为了解决这个问题,需要定义自己的复制构造函数,进行深拷贝。以下是`Tricycle`类的示例代码:
```cpp
#include <iostream>
class Tricycle
{
public:
Tricycle();
Tricycle(const Tricycle&);
~Tricycle();
int getSpeed() const { return *speed; }
void setSpeed(int newSpeed) { *speed = newSpeed; }
void pedal();
void brake();
private:
int *speed;
};
Tricycle::Tricycle()
{
speed = new int;
*speed = 5;
}
Tricycle::Tricycle(const Tricycle& rhs)
{
speed = new int;
```
0
0
复制全文
相关推荐









