[英]Why does my overloaded casting operator not have access to private members?
I'm attempting to implement an overloaded casting operator on my templated array2d
class using type T
.我正在尝试使用类型
T
在我的模板化array2d
类上实现重载的转换运算符。 So I'm to cast from array2d<T>
to a new array2d<E>
.所以我要从
array2d<T>
到一个新的array2d<E>
。
I'm able to perform the casting itself but problems arise when I try to set the casted data to the new instance of array2d<E>
.我能够自己执行转换,但是当我尝试将转换数据设置为
array2d<E>
的新实例时出现问题。 The compiler tells me that the casting operator doesn't have access to the private members of array2d
编译器告诉我,转换运算符无权访问
array2d
的私有成员
Here's where I am so far (edited out unrelated code for brevity)这是我到目前为止的位置(为简洁起见,编辑了不相关的代码)
array2d.h数组2d
template<typename T>
class array2d {
private:
// Member Variables
T** data;
size_t width, height;
public:
// constructors, methods, etc...
// Cast Operator
template<typename E>
operator array2d<E>() const;
};
// Other overloaded operators...
// Overloaded Casting Operator
template<typename T>
template<typename E>
array2d<T>::operator array2d<E>() const{
// Create new instance
array2d<E> castedArr(width, height);
// Allocate memory for the casted data, then cast each element
E** newData = new E*[castedArr.get_height()];
for (size_t i = 0; i < castedArr.get_height(); i++){
newData[i] = new E[castedArr.get_width()];
for (size_t j = 0; j < castedArr.get_width(); j++){
newData[i][j] = (E)data[i][j];
}
}
// issue here, can't set data because it's private.
castedArr.data = newData;
delete [] newData;
newData = nullptr;
return castedArr;
}
main.cpp主程序
#include "array2d.h"
int main(int argc, char *argv[]) {
// Cast Operator
// Create an array2d<T> of
// width = 5
// height = 5
// fill all elements with 42.1
array2d<double> x(5, 5, 42.1);
// Create a new array exactly the same as
// x, where x is casted to int
array2d<int> y = (array2d<int>) x;
return 0;
}
This confused me as I have many other overloaded operators that can access the private members just fine using practically the exact same logic.这让我很困惑,因为我有许多其他重载运算符,它们可以使用几乎完全相同的逻辑访问私有成员。
Why does this happen and what can I do to rectify it?为什么会发生这种情况,我可以做些什么来纠正它?
When writing a template, you don't nail down the actual type, you create a blue print for different types.编写模板时,您无需确定实际类型,而是为不同类型创建蓝图。
array2d<double>
and array2d<int>
are different types, and by default, two instances of two different classes can't access their private members. array2d<double>
和array2d<int>
是不同的类型,默认情况下,两个不同类的两个实例不能访问它们的私有成员。
You can fix that by declaring every instantiation of array2d
a friend class of the template array2d
:您可以通过将
array2d
的每个实例array2d
模板array2d
的友元类来解决这个array2d
:
template<typename T>
class array2d {
/* ... */
template<class E> friend class array2d;
/* ... */
};
As a side note, I'm not quite sure wheter作为旁注,我不太确定是否
delete [] newData;
is a good idea.是个好主意。 You're destroying parts of the resources that the new
array2d
instance is supposed to manage.您正在破坏新的
array2d
实例应该管理的部分资源。 If you delete[]
that again in array2d::~array2d()
, you'll have undefined behavior.如果在
array2d::~array2d()
中再次delete[]
,则会出现未定义的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.