如何使用任何类型的数组创建模板类并为其设置值?

How to create a template class with an array of any type and set values to them?

本文关键字:设置 建模 任何 何使用 类型 创建 数组      更新时间:2023-10-16

我正在研究一个名为 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 templateHotel class和其他classes分离到单独的模块-文件中。

Hotel对象中 您有一个默认构造函数,并且它有私有成员,但您无法设置这些值。您可以定义一个设置其成员的构造函数,也可以定义一个执行此操作的函数,您似乎SetHotel()但它不需要任何parameters

在您的class template中,您有多种选择,而不是使用rawT pointer

  • 你可以有一个类型为T{vector、list 等} 的container,如果你的集合相对较小,则只直接包含对象,例如:numTypes <= 100000.否则,您可以使用堆:见下文。
  • 或者,您可以有一个智能指针的向量shared_ptrunique_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的问题,你应该在一个单独的问题中询问它们。