如何使继承的基类使用公共变量

时间:2016-02-24 06:35:00

标签: c++ inheritance multiple-inheritance base-class

我想说我想为

创建一个界面
  1. Item具有可以阅读的内容。
  2. Item具有可以写入的内容。
  3. Item具有可以阅读和/或写入的内容
  4. 但是我不想创建3个抽象类,因为我可能希望传递一个Item,它是可读写的只接受可读项的函数。

    e.g。您只能阅读Book,因此Book会继承Readable,而Note可以读取和写入,因此会继承ReadWritable

    所以现在我可以创建这样的函数:

    std::string read(Readable& readable) const
    

    void take_a_note(Writable& writable)
    

    我有这个Readable抽象类

    template <typename ReadableType>
    class Readable
    {
    public:
        Readable(const ReadableType content);
        virtual ~Readable(void);
        virtual auto content(void) const noexcept -> const ReadableType&&;
    
    private:
        const ReadableType _content;
    };
    

    一个Writable抽象类

    template <typename WritableType>
    class Writable
    {
    public:
        Writable(void);
        virtual ~Writable(void);
        virtual void content(WritableType&& content) noexcept;
    
    private:
        WritableType _content;
    };
    

    但现在我想创建一个结合了这些功能的抽象类,称为ReadWritable

    template <typename ReadWritableType>
    class ReadWritable: public Readable<ReadWritableType>, public Writable<ReadWritableType>
    {
    public:
        ReadWritable(ReadWritableType content);
        virtual ~ReadWritable(void);
        using Writable<ReadWritableType>::content;
        using Readable<ReadWritableType>::content;
    };
    

    这就是问题所在,因为content(WritableType&& content)使用Writable::_content,而content(void)使用Readable::_content

    如何让他们使用相同的_content数据成员。如果我能做到这一点,有没有办法删除另一个?

    或者我的完整方法是错的?我对每一个意见持开放态度。

2 个答案:

答案 0 :(得分:1)

我不确定我的问题是否正确,但根据我的理解,您希望让两个单独的类共享相同的数据,并继续使用此共享数据继续使用它们。

我已经测试了几种组合,以下代码工作正常。但是我不得不做出一些改变:

#include <iostream>

class ReadableType
{
    public: int id=0;
            int* sharedValue;
};

class WritableType
{
    public: int id=1;
            int* sharedValue;
};

class ReadWritableType
{
    public: int id=2;
            int* sharedValue;
            ReadWritableType(int share){sharedValue = new int;*sharedValue = share;};
};

class Readable
{
public:
    void content(void){std::cout << "id: "<<_content->id <<" sharedValue: "<<*_content->sharedValue<<std::endl;};

protected:
    ReadableType *_content;
};


class Writable
{
public:
    void content(WritableType content){std::cout << "id: "<<_content->id <<" sharedValue: "<<*_content->sharedValue<<std::endl;};

protected:
    WritableType *_content;
};


class ReadWritable: public Readable, public Writable
{
public:
    ReadWritable(ReadWritableType* content);
    using Writable::content;
    using Readable::content;
private:
    void makeReadable(ReadWritableType* content);
    void makeWritable(ReadWritableType* content);
};

ReadWritable::ReadWritable(ReadWritableType* content)
{
    makeReadable(content);
    makeWritable(content);
}

void ReadWritable::makeReadable(ReadWritableType* content)
{
    Readable::_content = new ReadableType();
    Readable::_content->sharedValue = content->sharedValue;
}

void ReadWritable::makeWritable(ReadWritableType* content)
{
    Writable::_content = new WritableType();
    Writable::_content->sharedValue = content->sharedValue;
}
int main()
{
    ReadWritable x(new ReadWritableType(5));
    WritableType s;
    x.content();
    x.content(s);   
}

输出是每个_content都使用私有值(id)和共享值(sharedValue),它们在两个_contents之间共享相同的内存。

答案 1 :(得分:1)

使用额外的图层,您可以

template <typename ReadableType>
class IReadable
{
public:
    virtual ~IReadable() = default;
    virtual const ReadableType& content() const noexcept = 0;
};

template <typename WritableType>
class IWritable
{
public:
    virtual ~IWritable();
    virtual void content(WritableType&& content) noexcept = 0;
};

template <typename ReadableType>
class Readable : public IReadable
{
public:
    Readable(const ReadableType& content);
    virtual const ReadableType& content() const noexcept override;
private:
    const ReadableType _content;
};

template <typename WritableType>
class Writable
{
public:
    virtual void content(WritableType&& content) noexcept override;
private:
    WritableType _content;
};

最后:

template <typename ReadWritableType>
class ReadWritable: public IReadable<ReadWritableType>, public IWritable<ReadWritableType>
{
public:
    ReadWritable(ReadWritableType content);

    virtual void content(WritableType&& content) noexcept override;
    virtual const ReadableType& content() const noexcept override;
private:
    ReadWritableType _content;
};

您确实要重新实现ReadWritable::content,但可以使用Readable / Writable对代码进行分解并重复使用。