数组在C++中作为参数传递时隐式转换为容器类
Array converted implicitly to container class when passed as argument in C++
我正在做一个项目,在处理代码时,我遇到了以下特殊情况。
我有两节课。第一个在表示笛卡尔坐标的数组中保持三个浮点,并定义了获取这些点的方法;
class foo
{
protected:
float m_Coordinates[3];
public:
foo(float coordinates[3]);
void GetPoints(int resultArray[]);
};
foo::foo(int coordinates[3])
{
std::copy(coordinates, coordinates+3, m_Coordinates);
}
void foo::GetPoints(float resultArray[])
{
std::copy(m_Coordinates, m_Coordinates+3, resultArray);
}
第二个类也存储一个浮点数组,但它的构造函数使用foo作为包装类来传递值:
class bar
{
protected:
float m_MoreCoordinates[3];
public:
bar(foo f);
};
bar::bar(foo f)
{
f.GetPoints(m_MoreCoordinates);
//m_MoreCoordinates is passed by reference, so the values in
//m_MoreCoordinates are equal to the values in f.m_Coordinates
//after this line executes
}
请忽略这样一个事实:我对这段代码采取的方法简直太可怕了。它最初是一个使用数组的实验。将它们作为参数传递,将它们作为返回类型等
好的。在这里我发现了一些奇怪的东西。如果我声明一个浮点数组,并将它们作为参数传递给bar的构造函数,编译器将生成一个类foo的实例,并将其传递给bar。参见下面的示例代码:
int main(int argv, char** argc)
{
float coordinates[] = {1.0f, 2.1f, 3.0f};
//Here the compiler creates an instance of class foo and passes
//coordinates as the argument to the constructor. It then passes
//the resulting class to bar's constructor.
bar* b = new bar(coordinates);
//Effectively, the compiler turns the previous line into
//bar* b = new bar(foo(coordinates));
return 0;
}
当我看到这一点时,我认为这是代码中一个非常整洁的功能,并想知道它是如何发生的以及为什么发生的。这样做安全吗?我不明白它是如何工作的,所以我不想依赖它。如果有人能解释它是如何运作的,我真的很感激。
编辑:感谢Mankarse指出了转换将如何进行。最初,我有:
//Effectively, the compiler turns the previous line into
//bar* b = new bar(*(new foo(coordinates)));
正如您所猜测的,编译器正在隐式创建一个foo
对象并将其传递给bar
。通常,这被认为有点危险,因为foo
是在不知情的情况下构造的,为了避免这种情况,可以将foo
的构造函数声明为explicit
。在这种情况下,编译器不会从浮点数组中隐式创建foo
,您将得到一个编译器错误。
当你考虑它时,你会一直使用它。考虑以下内容:
void foo(std::string argument);
然后,假设您使用字符串文字调用此函数:
foo("argument");
这与相同
std::string argument("argument");
foo(argument);
这是一个非常有用的功能。
相关文章:
- 如何将AVFrame转换为类
- 如何正确实现容器类中元素的更改?
- 无法<string>从"常量字符 []"转换为<类名>
- C++ 用于检查容器类中是否存在函数和隐式推导规则的概念
- 从从该抽象类派生的 Python 对象强制转换C++抽象类C++
- 容器类中的类迭代器
- 如何将 std::array 转换为类类型
- 返回持有 std::mutex 锁的 RAII 容器类
- C++ 如何在容器类复制构造函数中复制分配器对象
- 如何使用迭代器设计模式为 C++ 中的包装容器类提供多个迭代器?
- 如何在模板容器类中实现复制构造函数和赋值运算符
- 通过点之间的"dynamic_cast"转换为类
- 将类强制转换为类
- 将结构转换为类
- 将一组类转换为类模板并避免构造函数模棱两可
- 如何转换一个类方法以修改另一个类的私有元素?
- C++自动类型转换:容器类的错误行为
- 如何使用指针C++将类的析构函数转换为另一个类?类似于单向列表容器类和节点类
- 数组在C++中作为参数传递时隐式转换为容器类
- 如何将可变参数强制转换为容器类