如何使用任何类型的数组创建模板类并为其设置值?
How to create a template class with an array of any type and set values to them?
我正在研究一个名为 Collection 的模板类,该类仅将数组作为模板类的私有成员。我还需要实现其他类,但现在我正在研究其中一个称为 Hotel。
我应该创建一个像template class Type,int p_
这样的模板,所以当我在主功能中调用Collection Hotel,5 h;
时,它应该创建一个类型的酒店数组,对吗?但是当我尝试这样做时,它只是创建一个酒店类型的对象并调用它的 deafult constuctor。
我可以使用一些建议来设置对象的值。酒店类有char* HotelName;int numRooms,openedYear,catagoryHotel
。
template <class Type,int p>
class Collection
{
private:
Type* niz;
int n;//added this just in case
public:
Collection();
~Collection();
bool Find(Type t);//checks if the t is in the array with operator!=
void Arrange();//arranges the array
void Reverse();//swithces the first memmber of array to the last and the second to p-2
void Set(int i,Type t);//should set t to the niz[i]
void SaveElement(int i, char* Dat);//this is for saving in a file
void ReedElement(char*Dat);//this is for printing on the screan from a file
//bool operator!=(Type& t,Type& a);//visual studio is telling me 2 many arguments,dont know why
};
class Hotel
{
private:
char*HotelName;
int numRooms, openedYear,catagoryHotel;
public:
Hotel();
~Hotel();
void SetHotel();
//bool operator <=(Hotel& h1, Hotel& h2);this for some reason doesnt work also
};
//In my constructor for Collection i typed
template<class Type, int p>
inline Collection<Type, p>::Collection()
{
n = p;
niz = new Type[n];
}
//in the constructor for Hotel() i typed
Hotel::Hotel()
{
HotelName = "Undenfined";
catagoryHotel = 0;//how much stars it has
openedyear = 0;
numRooms = 0;
}
这是我为酒店输入的全部内容,在我清除之前,我不想再进一步了。
在查看代码后,从设计过程的角度来看,查看代码时;您应该将Collection class template
、Hotel class
和其他classes
分离到单独的模块-文件中。
在Hotel
对象中 您有一个默认构造函数,并且它有私有成员,但您无法设置这些值。您可以定义一个设置其成员的构造函数,也可以定义一个执行此操作的函数,您似乎SetHotel()
但它不需要任何parameters
。
在您的class template
中,您有多种选择,而不是使用raw
T pointer
。
- 你可以有一个类型为
T
{vector、list 等} 的container
,如果你的集合相对较小,则只直接包含对象,例如:numTypes <= 100000
.否则,您可以使用堆:见下文。 - 或者,您可以有一个智能指针的向量
shared_ptr
或unique_ptr
在这种情况下,shared_ptr
会更合适。这使得内存管理更清晰、更易于阅读、内存泄漏、悬空指针、无效引用等的可能性更小。
上述类的示例:
酒店.h
#ifndef HOTEL_H
#define HOTEL_H
#include <string>
class Hotel {
private:
// char* HotelName; // replace with std::string
std::string hotelName_;
unsigned numRooms_; // changed to unsigned since you would not have negative rooms.
unsigned openingYear_; // same as above for unsigned as well as made the variable name make more sense.
unsigned hotelRating_; // same as above for unsigned, also changed name to rating
public:
Hotel(); // default constructor okay
Hotel( const std::string& hotelName, unsigned numRooms, unsigned rating );
~Hotel() = default; // default destructor okay: set to default - not managing memory via new & delete
void setHotel( const std::string& hotelName, unsigned numRooms, unsigned rating );
};
#endif // !HOTEL_H
酒店.cpp
#include "Hotel.h"
// Hotel() - Default Constructor - Enables you to instantiate an "empty" object
Hotel::Hotel() :
hotelName_( "Undefined" ),
numRooms_( 0 ),
rating_( 0 ) {
}
// Hotel( ... ) - User Defined - Enables you to instantiate an object that requires known data
Hotel::Hotel( const std::string& hotelName, unsigned numRooms, unsigned rating ) :
hotelName_( hotelName ),
numRooms_( numRooms ),
raiting_( rating ) {
}
// ~Hotel() - Default Destructor - Defaulted in header file
void Hotel::setHotel( const std::string& hotelName, unsigned numRooms, unsigned rating ) {
hotelName_ = hotelName;
numRooms_ = numRooms;
rating_ = rating;
}
收藏.h
#ifndef COLLECTION_H
#define COLLECTION_H
#include <vector>
#include <memory>
template<class Type, int p>
class Collection {
private:
std::vector<std::shared_ptr<Type>> collection_;
int n;//added this just in case
public:
Collection();
~Collection();
bool find( Type t );
void arrange(); // without any parameters how do you plan to arrange your container?
void reverse();
// void set( int i, Type t ); // since I decided to use std::vector<...> changed this to addElement( Type t );
void addElement( Type t );
void saveElement( int i, const std::string& dat );
void readElement( const std::string& dat );
//bool operator!=(Type& t,Type& a); //visual studio is telling me 2 many arguments,dont know why
};
template<class Type, int p>
inline Collection<Type, p>::Collection() {
// don't need to do anything since you have
// an empty std::vector<std::shared_ptr<Type>>
}
template<class Type, int p>
inline Collection<Type, p>::addElement( Type t ) {
collection_.emplace_back( new Type( t ) ); // Psudeo
}
#endif // !COLLECTION_H
这只是从设计的角度来看,至于你的overloaded operators
的问题,你应该在一个单独的问题中询问它们。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++/Linux中设置单调时钟的一些技巧
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 嵌套在类中时无法设置成员数据
- 需要帮助设置在C++中使用的Potrace
- 如何在自删除后将对象设置为nullptr
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何在Ubuntu中使用cmake设置qt4
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 如何在boost beast http请求中设置http头
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 如何在24位SDL_Surface上设置像素的颜色
- std::设置自定义比较器
- 如何设置一个范围来提取我想要获得的信息
- 如何在C/C++中用FD_set Unix设置套接字文件描述符
- 通过选项卡的文本设置QTabWidget顺序
- 将特征矩阵的向量设置为0
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?