你能禁止我设计和使用这个集装箱吗
would you prohibit me from this container design and use?
#include <iostream>
using namespace std;
//I want such a container that holds an array and don't want to expose array directly
template <size_t T>
class Container{
public:
Container(int in[]);
int getValue(const unsigned int pos);
//void setValue(const unsigned int pos,const int value);
unsigned int getSize();
/*...
...
...*/
unsigned int theSize;
int theArray[T];
};
template <size_t T>
Container<T>::Container(int in[]){
theSize=T;
for (unsigned int i=0;i<T;i++)
theArray[i]=in[i];
}
template <size_t T>
int Container<T>::getValue(const unsigned int pos){return theArray[pos];}
template <size_t T>
unsigned int Container<T>::getSize(){
return theSize;
}
//then I want to pass objects of this type around by address, but then to use that address //I have to do such design as follows, now would you prohibit me from doing this?
void someFunc(void * in){
Container<1> * ptr=reinterpret_cast<Container<1> *>(in);
unsigned int times=ptr->getSize();
for (unsigned int i=0;i<times;i++)
cout <<ptr->getValue(i)<<' ';
cout <<'n';
}
int main(int argc,char ** argv){
int araye[10]={1,2,3,4,5,6,7,8,9,10};
Container<10> obj(araye);
someFunc(&obj);
cin.get();
}
是的,我会禁止您实现这个容器。你正在重新发明轮子,据我所知,没有理由这么做。
请改用std::array
。即使您不能使用C++11(array
就是在这里引入的),您也应该使用像std::vector
这样已经设计、构建和测试过的东西。
相关文章:
- ISO C++禁止声明没有类型的"setInputNombre"
- 禁止显示由于常量为零而比较始终为假的警告
- 禁止在控制台上记录谷神星
- 禁止显示有关包含文件中 #pragma 包的警告
- 禁止指针和整数之间的比较C++
- ISO C++禁止指针和整数 [-fpermissive] [c++] 之间的比较
- 为什么我会收到此警告:ISO c++ 禁止可变长度数组"v"[-Wvla]
- 当空基类也是成员变量时,为什么禁止空基优化?
- ISO 中禁止可变长度数组 C++崇高文本中的错误 3.
- 标记类以禁止操作
- 禁止子函数调用父级的抽象(或虚拟)函数
- Linux C 只禁止我的程序使用核心转储
- 错误:ISO C++禁止可变长度数组"subVec"[-Werror=vla]
- 如何禁止在 g++ 中使用但从未定义警告的内联函数
- 捕获/禁止发送到 std::cout 的 OpenCV 警告
- Qt 错误 iso c++ 禁止指针和整数之间的比较 -permissive
- 为什么禁止建造 istreams?
- 是否可以禁止在for循环体内部修改循环变量
- 在c++中,如果首先禁止默认构造,那么禁止复制构造有意义吗
- 你能禁止我设计和使用这个集装箱吗