using objects of two or more data types as key for

2019-09-19 12:05发布

I have a class that contains a map. Earlier, there were two maps, one with char key and one with string key. I created a class that contains enum and union and two constructors, one from char and one from string, overloaded comparison operator and used that class as key.

But I keep wondering. could this have been solved with clever use of inheritance instead?

edit:

My class is something along the lines:

class A
{
    enum B
    {
        Ax, Ay
    } b;
    union C
    {
        X x;
        Y y;
    } c;
 public:
    A(X x) : b(Ax), c(x) {}
    A(Y y) : b(Ay), c(y) {}
    bool operator<(A& rhs)
    {
        if(this->b == rhs.b)
        {
            if(this->b == Ax) return this->c.x < b.c.x;
            if(this->b == Ay) reutrn this->c.y < b.c.y;
        }
        // if the "type" is different, I assume values are different
        // and all I need is any strong ordering, actual order doesn't matter
        return this->b < rhs.b;
    }
};

Later on I can use it fe. like that:

class Q
{
    // ...
public:
    Q(vector<A> v)
    {
        // don't worry about this code, I haven't figured out how c++11 for each
        // works exactly yet, but it's not important, I can do that myself
        for each (auto a in v)
        {
            // ...
        }
    }
};

Now using curly brackets I can initialize Q with various A, and I can create distinct A from char or string.

My class, Aaccomplished this by containing union for the actual data required to compare keys of one type, and enum used to compare keys of different types. Basic inheritance tree with virtual base class and two subclasses would result in every instance containing the same amount of information - different internal variables would take role of the union, and different actual classes would take the role of the enum. But I just can't come up with a way to do it properly without breaking any important principles.

标签: c++ oop
2条回答
放我归山
2楼-- · 2019-09-19 12:29

Why not use boost::variant or std::variant once the latter becomes available in your compiler?

It feels like the most natural solution to the problem and makes the code very readable:

#include <boost/variant.hpp>

#include <string>
#include <map>
#include <iostream>

int main()
{
    using Key = boost::variant<char, std::string>;
    using Value = int; // whatever
    std::map<Key, Value> map;
    map['A'] = 1;
    map["B"] = 2;
    map["A"] = 3;

    for (auto&& key_value : map)
    {
        std::cout << key_value.first << " => " << key_value.second << '\n';
    }
}

Output:

A => 1
A => 3
B => 2
查看更多
Melony?
3楼-- · 2019-09-19 12:34

Although generally you can simplify union code with inheritance, it does not help much when you need mixed group of objects to have behavior that is aware of all other implementations.

You can make a base class, and have two subclasses - for string-based keys and for char-based keys. However, the comparison operator of string-based keys must know how to work with char-based keys, and the operator for char-based keys must know how to work with string-based keys. In situations like that the code becomes "monolithic", making a single class alternative more preferable.

In your situation, a class that exposes two constructors, and maintains a string internally should be an easy alternative:

class MyKey {
    string key;
    int subtype;
public:
    MyKey(const string& s) : key(s), subtype(1) {}
    MyKey(char c) : key(1, c), subtype(2) {}
    bool operator<(MyKey& other) {
        return subtype==other.subtype
             ? (key < other.key)
             : (subtype < other.subtype);
    }
};
查看更多
登录 后发表回答