构造函数的输入参数可以引用类吗
时间: 2023-10-08 14:03:56 浏览: 33
是的,构造函数的输入参数可以引用类。在这段代码中,`AStarExpansion` 的构造函数 `AStarExpansion(PotentialCalculator* p_calc, int nx, int ny)` 的第一个参数 `p_calc` 是一个指向 `PotentialCalculator` 类对象的指针。通过使用指针作为参数,可以在构造函数中传递对 `PotentialCalculator` 对象的引用。
使用引用类作为构造函数的参数可以方便地在类之间共享数据或者操作其他类的成员函数。这样可以提高代码的可读性和可维护性,并且避免了不必要的数据复制。
相关问题
拷贝构造函数讲解(附例题代码)
拷贝构造函数是一种特殊的构造函数,在创建一个新对象时,它会从另一个已存在的对象中复制构造函数参数。其函数原型为:
```
类名(const 类名 &obj)
```
其中,参数列表中的 "const" 关键字表示输入参数为只读, "&" 符号表示传递的是引用类型,即万一传进来的是个 reference ,就不会产生歧义。
以下是一个拷贝构造函数的示例代码:
```
#include <iostream>
using namespace std;
class Box {
public:
// 通常的构造函数
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
// 拷贝构造函数
Box(const Box &B) {
cout << "Copy constructor called." << endl;
length = B.length;
breadth = B.breadth;
height = B.height;
}
double Volume() {
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main() {
Box Box1(3.3, 1.2, 1.5); //声明对象时调用了通常的构造函数
Box Box2(Box1); //声明对象时调用了拷贝构造函数
// 输出 Box1 的体积
cout << "Box1 的体积:" << Box1.Volume() << endl;
// 输出 Box2 的体积
cout << "Box2 的体积:" << Box2.Volume() << endl;
return 0;
}
```
在上面的代码中,我们声明了一个 Box 类,并声明了一个常用的构造函数和一个拷贝构造函数。这里的 Box1 对象调用了常用的构造函数,而 Box2 对象则在声明时调用了拷贝构造函数。最后,通过 Volume() 函数计算 Box1 和 Box2 对象的体积并输出。
希望这个示例能帮助你理解拷贝构造函数的概念。
建立一个分数类。分数类的数据成员包括分子和分母,操作包括约分、通分、比较、加、减、乘、除、求倒数、显示和输入,并将其集合类和分数类的集合类型或分数类型参数改为引用,并添加复制构造函数。
好的,以下是建立一个分数类的代码:
```cpp
#include <iostream>
using namespace std;
class Fraction {
private:
int numerator; // 分子
int denominator; // 分母
public:
Fraction(int num = 0, int den = 1) : numerator(num), denominator(den) {}
// 约分
void reduce() {
int gcd = __gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
}
// 通分
void commonDenominator(Fraction& other) {
int lcm = denominator * other.denominator / __gcd(denominator, other.denominator);
numerator *= lcm / denominator;
denominator = lcm;
other.numerator *= lcm / other.denominator;
other.denominator = lcm;
}
// 比较
bool operator<(const Fraction& other) const {
return numerator * other.denominator < other.numerator * denominator;
}
bool operator==(const Fraction& other) const {
return numerator * other.denominator == other.numerator * denominator;
}
// 加
Fraction operator+(const Fraction& other) const {
Fraction res(numerator, denominator);
res.commonDenominator(const_cast<Fraction&>(other));
res.numerator += other.numerator;
res.reduce();
return res;
}
// 减
Fraction operator-(const Fraction& other) const {
Fraction res(numerator, denominator);
res.commonDenominator(const_cast<Fraction&>(other));
res.numerator -= other.numerator;
res.reduce();
return res;
}
// 乘
Fraction operator*(const Fraction& other) const {
Fraction res(numerator * other.numerator, denominator * other.denominator);
res.reduce();
return res;
}
// 除
Fraction operator/(const Fraction& other) const {
Fraction res(numerator * other.denominator, denominator * other.numerator);
res.reduce();
return res;
}
// 求倒数
Fraction reciprocal() const {
return Fraction(denominator, numerator);
}
// 显示
void display() const {
cout << numerator << "/" << denominator << endl;
}
// 输入
void input() {
cin >> numerator >> denominator;
reduce();
}
};
// 复制构造函数
Fraction::Fraction(const Fraction& other) {
numerator = other.numerator;
denominator = other.denominator;
}
// 集合类
template <typename T>
class Set {
private:
T* data;
int size;
public:
Set(int s = 0) : size(s) {
data = new T[size];
}
Set(const Set& other) : size(other.size) {
data = new T[size];
for (int i = 0; i < size; i++) {
data[i] = other.data[i];
}
}
~Set() {
delete[] data;
}
Set& operator=(const Set& other) {
if (this != &other) {
delete[] data;
size = other.size;
data = new T[size];
for (int i = 0; i < size; i++) {
data[i] = other.data[i];
}
}
return *this;
}
void insert(const T& val) {
for (int i = 0; i < size; i++) {
if (data[i] == val) {
return;
}
}
T* temp = new T[size + 1];
for (int i = 0; i < size; i++) {
temp[i] = data[i];
}
temp[size++] = val;
delete[] data;
data = temp;
}
void remove(const T& val) {
for (int i = 0; i < size; i++) {
if (data[i] == val) {
T* temp = new T[size - 1];
for (int j = 0; j < i; j++) {
temp[j] = data[j];
}
for (int j = i + 1; j < size; j++) {
temp[j - 1] = data[j];
}
size--;
delete[] data;
data = temp;
return;
}
}
}
void display() const {
for (int i = 0; i < size; i++) {
cout << data[i] << " ";
}
cout << endl;
}
};
int main() {
Fraction f1(2, 3), f2(3, 4), f3(4, 5);
f1.display();
f2.display();
f3.display();
Fraction f4 = f1 + f2;
f4.display();
Fraction f5 = f1 - f2;
f5.display();
Fraction f6 = f1 * f2;
f6.display();
Fraction f7 = f1 / f2;
f7.display();
Fraction f8 = f1.reciprocal();
f8.display();
Set<Fraction> s1(2);
s1.insert(f1);
s1.insert(f2);
s1.insert(f3);
s1.display();
s1.remove(f2);
s1.display();
Set<Fraction>& s2 = s1;
s2.insert(f4);
s2.display();
Set<Fraction> s3 = s1;
s3.display();
return 0;
}
```
希望这个代码能够满足你的需求!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)