简体   繁体   中英

C++ exceptions with unused variables

I have a map class template in which I store my generic data type. I have to use strings for indexing the map class. I need to throw an uninitialized exception if attempt to access an uninitialized element. The main looks like this:

mapClass <double> mc;

mc[”aaa”] = 3.5;

double var1 = mc[“aaa”];

cout << var1 << endl;  //print 3.5

try{

    double var2 =  mc[“aab”];   //uninitialized error: throw exception

}catch( classMap<double>::Uninitialized&){ 

    cout<< ”Uninitialized error…..”<<endl;

}

My idea is to create a node class inside the mapClass template, which will store key (string) and value (T) elements. I have implemented an operator[] overload that will return a reference to the T element. This operator[] overload will also push_back new elements if the given string doesn´t exist inside the vector.

template< typename T >

class mapClass{

public:

    class Uninitialized{};

    class Node{
    public:
        string key;
        T value;
        bool asigned;

        Node(){
            key = "";
            asigned = false;
            }
        ~Node(){}
    };

    mapClass(){
        initialized=false;
        max=0;
    }

    T& operator[](string a){
        int ret;
        bool out = false;
        if (!initialized){
            Node newNode;
            nodeArray.push_back(newNode);
            nodeArray[0].key = a;
            initialized = true;
            max = 0;
            ret = 0;
        }
        else{
            for (int i = 0; i <= max; i++){
                if (nodeArray[i].key == a){
                    out = true;
                    ret = i;
                }
            }
            if (!out){
                max++;
                ret = max;
                Node newNode;
                nodeArray.push_back(newNode);
                nodeArray[max].key = a;
            }
        }
        return nodeArray[ret].value;
    }

private:

    vector<Node> nodeArray;
    bool initialized;
    int max;
};

Right now the program prints the value 3.5, but also prints a value (which moves around -8578623) instead of throwing an exception. The problem is that I don´t know in which part of the code should I throw the exception and I don´t know the way to check if a variable is uninitialized. I´d appreciate any kind of help.

Your current operator[] does not distinguish between read access and original creation of an item. Thus, as-is it can't throw an exception for use of a non-existing item, because as far as it's concerned there's no difference from original creation of that item.

There are various technical solutions (none really good) within the constraints of the existing design, in particular

  • you can let operator[] return a proxy object whose assignment operator creates an item, and whose conversion to to the item type accesses an existing item.

But seeing as the given code still has very fundamental problems, I would not recommend this.

Instead, ditch the current design. Either make access of non-existing item default to creating a new item, or make the operations of creation and access distinct. The former is what std::map does, the latter is perhaps easiest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM