首页 > 解决方案 > How to design extensible multiple inheritance?

问题描述

I have yet another question about multiple inheritance design which has an answer i.e. here (but focused on footprint) or here (too vague), but most answers I stumbled upon is emphasizing the performance drawbacks. However (as Bjarne Stroustrup claims here) it is a language feature which should be prefered to workarounds. Here is a longer example to ilustrate the question which follows the example:

The Example

In Czech Republic, the birth number (SSN equivalent) is assigned in this format: YYMMDDXXX, so let's have a class to get the birth date in standard D.M.YYYY:

class Human {
protected:
  char output[11];
  char input[10];

public:
  Human (const char* number) {
    strncpy(input, number, 10);
    if(!number[10]) throw E_INVALID_NUMBER;
  }

  static int twoCharsToNum(const char* str) {
    if(!isdigit(str[0]) || !isdigit(str[1])) throw E_INVALID_NUMBER;
    return (str[0]-'0')*10 +str[1]-'0';
  }

  const char* getDate() {
    sprintf(output, "%d.%d.%d", getDay(), getMonth(), getYear());
    return output;
  }

  // range check omitted here to make code short
  virtual int getDay() { return twoCharsToNum(input+4); }
  virtual int getMonth() { return twoCharsToNum(input+2); }
  virtual int getYear() { return twoCharsToNum(input)+1900; }
};

Three methods are virtual, because females got +50 to their month. So let's inherit Man and Woman classes to get the date properly:

class Man : public Human {
public:
  using Human::Human;
};

class Woman : public Human {
public:
  using Human::Human;
  int getMonth() {
    int result = twoCharsToNum(input+2)-50;
    if(result<0) throw E_INVALID_GENDER;
    if(result==0 || result>12) throw E_INVALID_RANGE;
    return result;
  }
};

Since 1954 the number has 4 digits appendix, not 3 (there is a sad story behind this mentioned at the end of this question). If the library was written in 1944, ten years later somebody can write a Facade to get the birth date correctly for future millenials:

class Human2 : public Human {
public:
  using Human::Human;
  virtual int getYear() {
    int year = twoCharsToNum(input);
    if(year<54 && strlen(number)==10) year+= 2000;
    else year+= 1900;
    return year;
  }
};

class Man2 : public Human2 {
public:
  using Human2::Human2;
};

In class Woman2 we need the Woman::getMonth method, so we need to solve the diamond problem:

class Human2 : virtual public Human { ... };
class Woman  : virtual public Human { ... }; // here is the real issue
class Woman2 : public Human2, public Woman {
  using Human2::Human2;
  using Woman::Woman;
};

The Diamond problem diagram:

    Woman2
    ^    ^
    |    |
Woman    Human2
    ^    ^
    |    |
    Human 

The Question

The issue is that Human, Man and Woman could be in a form of binary library where the client code can not rewrite the inheritance to virtual. So how to properly design extensible library to enable multiple inheritance? Should I make every inheritance in the library scope virtual (since I don't know in advance how it could be extended), or is there any more elegant universal design?

Regarding the performance: isn't this the domain of low-level programming and compiler optimization, shouldn't the design perspective prevail on high level programming? Why compilers don't auto virtualize inheritance like they do in RVO or inline calls decisions?

The sad story behind the example

In 1954 some technically inspired bureucrat decided that tenth cipher will be added in a way so the number will be divisible by 11. Later the genius figured out that there are numbers which can not be modified this way. So he issued an exception that in these cases the last number will be zero. Later that year an internal directive was issued, that no such exceptions will be allowed. But in the meantime some 1000+ birth numbers were issued which are not divisible by 11, but still legal. Regardless of this mess, the century of the year can be inferred by the number length until 2054, when we will experience Y2K revival. Alas, there is also a common practice that immigrants born before 1964 get assigned a 10-digit birth number.

标签: c++multiple-inheritanceextensibility

解决方案


如果您无法编辑原始库,您可以尝试通过“mixin”来解决它,即新的外观类由它们自己的基类ManWoman.

例如:

#include <iostream>
#include <system_error>
#include <cstring>
#include <memory>
#include <type_traits>

class Human {
protected:
    char output[11];
    char input[10];

public:
    Human (const char* number) {
        memcpy(input, number, 10);
        if(!number[10])
            throw std::system_error( std::make_error_code( std::errc::invalid_argument ) );
    }

    static int twoCharsToNum(const char* str) {
        if(!isdigit(str[0]) || !isdigit(str[1]))
            throw std::system_error( std::make_error_code( std::errc::invalid_argument ) );
        return (str[0]-'0')*10 +str[1]-'0';
    }

    const char* getDate() {
        sprintf(output, "%d.%d.%d", getDay(), getMonth(), getYear());
        return output;
    }

    // range check omitted here to make code short
    virtual int getDay() {
        return twoCharsToNum(input+4);
    }
    virtual int getMonth() {
        return twoCharsToNum(input+2);
    }
    virtual int getYear() {
        return twoCharsToNum(input)+1900;
    }
};

class Man:public Human {
public:
    Man(const char* number):
        Human(number)
    {}
};

class Woman : public Human {
public:
    Woman(const char* number):
        Human(number)
    {}
    virtual int getMonth() override {
        int result = Human::twoCharsToNum(input+2)-50;
        if(result<0)
            throw std::system_error( std::make_error_code( std::errc::invalid_argument ) );
        if(result==0 || result>12)
            throw std::system_error( std::make_error_code( std::errc::invalid_argument ) );
        return result;
    }
};

template<class GenderType>
class Human_Century21:public GenderType {
public:

    explicit Human_Century21(const char* number):
        GenderType(number)
    {
        // or use std::enabled_if etc
        static_assert( std::is_base_of<Human,GenderType>::value, "Gender type must inherit Human" );
    }

    virtual int getYear() override {
        int year = Human::twoCharsToNum(this->input);
        if(year<54 && std::strlen(this->input) == 10 )
            year += 2000;
        else
            year += 1900;
        return year;
    }
};


int main ()
{
    auto man = std::make_shared< Human_Century21<Man> >(  "530101123"  );
    std::cout << "Man: [ year: " << man->getYear() << ", month:" << man->getMonth() << " ]" << std::endl;
    auto woman = std::make_shared< Human_Century21<Woman> >( "54510112345" );
    std::cout << "Woman: [ year: " << woman->getYear() << ", month:" << woman->getMonth() << " ]" << std::endl;
    return 0;
}

输出:

Man: [ year: 1953, month:1 ]
Woman: [ year: 1954, month:1 ]

Aniway,你最好重新设计所有这些类,恕我直言,最好的选择 - 将日期存储为整数或 std::chrono 类型和性别作为枚举字段。提供额外的工厂方法来解析日期格式字符串,并将依赖项仅注入人类类。


推荐阅读