操作员过载到preety print枚举类

时间:2018-03-31 22:56:07

标签: c++

我正在尝试打印枚举类(将使用某种地图打印汽车,房屋)。

在下面的代码中,我应该将函数粘贴到operator over<< ?

PS:这是一个人为的例子。

#include <iostream>
#include <vector>
#include <memory>

enum class property_type {CAR, HOUSE};

class Property
{
    public:
    virtual property_type type() = 0;
};

class Car : public Property
{
    public:
    Car(std::string name) : mName(name) {}
    std::string mName;
    property_type type() {return property_type::CAR;}
};

class House : public Property
{
    public:
    House(std::string name) : mName(name) {}
    std::string mName;
    property_type type() {return property_type::HOUSE;}
};

class Person
{
    public:
    std::vector< std::shared_ptr<Property> > properties;
};

int main()
{
    Person x;
    auto y = std::shared_ptr<Property>(new Car("my car"));
    auto z = std::shared_ptr<Property>(new House("my house"));
    x.properties.push_back(y);
    x.properties.push_back(z);
    for (auto i : x.properties) {
            std::cout << i->type() << std::endl;
    }
    return 0;
}  

1 个答案:

答案 0 :(得分:1)

你可能可以使用X Macro来使它整洁和可扩展,如下所示:

using namespace std;

template<class T>
class bar
{
public:
    template<class... Args>
    static bar<T> CreateBook(Args&&... args)
    {
        return bar<T>(std::forward<Args>(args)...); // Don't compile here
    }

    T get()
    {
        return val;
    }
private:

    bar(const T& t) :val(t) {}
    T val;

};

struct book
{
    string name;
    int phone;
    book(string s, int t) : name(s), phone(t) {}
};

void print(bar<book> k)
{
    cout << k.get().name << " " << k.get().phone << endl;
}

int main()
{
    bar<book> b = bar<book>::CreateBook("Hello World",91520);

    print(b);

    return 0;
}

演示:https://ideone.com/BqB1x8

输出:

#include <iostream>
#include <vector>
#include <memory>

#define PROPERTY_TYPES \
    X(CAR, "A Car") \
    X(HOUSE, "A House") \
    X(ISLAND, "An Island") // <-- add more types

#define X(type, name) type, 
enum class property_type : size_t
{
    PROPERTY_TYPES
};
#undef X

#define X(type, name) name,
const char *property_name[] =
{
    PROPERTY_TYPES
};
#undef X

class Property
{
public:
    virtual property_type type() = 0;
};

class Car : public Property
{
public:
    Car(std::string name) : mName(name) {}
    std::string mName;
    property_type type() { return property_type::CAR; }
};

class House : public Property
{
public:
    House(std::string name) : mName(name) {}
    std::string mName;
    property_type type() { return property_type::HOUSE; }
};

class Person
{
public:
    std::vector< std::shared_ptr<Property> > properties;
};

std::ostream& operator<< (std::ostream& os, property_type type)
{
    os << property_name[static_cast<size_t>(type)];
    return os;
}

int main()
{
    Person x;
    auto y = std::shared_ptr<Property>(new Car("my car"));
    auto z = std::shared_ptr<Property>(new House("my house"));
    x.properties.push_back(y);
    x.properties.push_back(z);
    for (auto i : x.properties)
    {
        std::cout << i->type() << std::endl;
    }
    return 0;
}