当构造函数上只有一个参数时,会对参数进行阴影处理
shadows a parameter when single parameter on constructor
Hi我在web中编写了一个简单的类,然后是示例代码。此代码运行良好,没有错误。
class Shape{
protected:
int width,height;
public:
Shape(int a = 0, int b=0)
{
width = a;
height = b;
}
};
class regSquare: public Shape{
public:
regSquare( int a=0, int b=0)
{
Shape(a, b);
}
};
但是当我把我的改为只有一个构造函数的参数时,比如
class Shape{
protected:
int width;
public:
Shape(int a = 0)
{
width = a;
}
};
class regSquare: public Shape{
public:
regSquare(int a = 0)
{
Shape(a);
}
};
此按摩发生错误
'错误:"a"的声明遮蔽了参数'
我不知道我的代码出了什么问题
不过,很可能这两个版本都不符合您的要求!代码
regSquare(int a = 0, int b = 0) {
Shape(a, b);
}
是否初始化regSquare
对象的Shape
子对象!相反,它使用参数a
和b
创建一个类型为Shape
的临时对象。单参数版本的作用类似:
Shape(a);
定义了一个默认构造的类型为Shape
的对象,称为a
。您可能打算使用初始值设定项列表将构造函数参数传递给Shape
子对象,例如:
reqSquare(int a = 0, int b = 0)
: Shape(a, b) {
}
或
regSquare(int a = 0)
: Shape(a) {
}
因为在单参数中,编译器将其作为对象名称并创建对象,所以它正在创建冲突。
相关文章:
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- 如何编写具有相同名称的相同函数,该函数在C++中几乎以相似的方式处理不同的类参数?
- 为什么这个噪声函数不处理否定参数?
- 容器如何处理 lambda 表达式的参数
- 处理一般情况混合类型和非类型的可变参数模板
- 如何在 c++ 窗口中将参数和返回的退出值传递到批处理文件/从批处理文件获取返回的退出值
- C++17.处理使用 auto 相关的模板参数.代码排序困难
- 使用qprocess将参数传递给批处理
- 专门处理一个参数(C++模板)的两个模板参数
- 编译器如何处理<Type> <Type> 具有多参数的向量initializer_list
- 当 C++ 中函数参数的输入类型(类)错误时的错误处理
- 在处理之前评估宏参数
- 如何处理C++98中不同类型的多个参数?
- 有没有办法帮助编译器处理复杂的参数推导?
- 如何处理模棱两可的模板参数
- 将不同类型的多种参数发送到处理方法的最佳方法.C
- 巩固大量参数处理的干净方法是什么
- 通过c++主字符**参数处理不同字符串编码的正确方法是什么
- 当函数指向一个函数时,如何将函数作为参数处理
- 为什么我的命令行参数处理崩溃我的程序