dimanche 5 juillet 2020

Initialize members of a class in C++

I have a class with the following declaration:

class OrderBook {
        std::string OrderBookType;
        std::map<double, vector<float*>> OrderBookData;
        std::unordered_map<int, float*> OrderPool;
        
        public:
                OrderBook(string);
} 

I have 2 questions regarding the class initialization:

  1. I would like to specify the comparator for the OrderBook map based on OrderBookType. Is the following valid/good programming practice?
OrderBook::OrderBook(string bookType) {
        
        OrderBookType = bookType;
        
        if (bookType == 'B') {
                OrderBookData = std::map<float, vector<float*>, std::greater<float>>
        }
        else {
                OrderBookData = std::map<float, vector<float*>, std::less<float>>
        }

        OrderPool;
}   
  1. How do I initialize an empty map for OrderPool? To make sure OrderPool is an empty unordered map, do I have to do the following:
OrderPool = std::unordered_map<int, float*>()

or is OrderPool; sufficient?

Aucun commentaire:

Enregistrer un commentaire