Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 关于面向对象类的概念_C++_Oop - Fatal编程技术网

C++ 关于面向对象类的概念

C++ 关于面向对象类的概念,c++,oop,C++,Oop,我对OOP类的一些概念有疑问,假设我有以下类: Parser类,其中包含用于解析流数据的数据 String类,包括用于分析流数据的字符串操作 包含用于解析流数据的整数操作的Integer类 所以String和Integer类继承解析器类,因为它们都需要关于流的特定信息,如位置、长度等 当我有一个同时使用字符串和整数函数的函数时,问题就来了 让我们把这个新函数放在一个名为MultipleOperations的类中。多重操作需要String和Integer类,所以它同时继承这两个类,但Strin

我对OOP类的一些概念有疑问,假设我有以下类:

  • Parser类,其中包含用于解析流数据的数据
  • String类,包括用于分析流数据的字符串操作
  • 包含用于解析流数据的整数操作的Integer类
所以String和Integer类继承解析器类,因为它们都需要关于流的特定信息,如位置、长度等

当我有一个同时使用字符串和整数函数的函数时,问题就来了

让我们把这个新函数放在一个名为MultipleOperations的类中。多重操作需要String和Integer类,所以它同时继承这两个类,但String和Integer类已经继承了解析器,所以当尝试从解析器类访问某些数据时是不明确的

另一方面,如果set String和Integer类由多个操作组成,那么我将无法访问解析器类

此外,我不太理解“hasaa”的概念,因为在大多数情况下,我需要引用基类中的数据,因此它是一个“is a”

下面是我的问题的一个例子:

class Parser{
private:
    int errorcode;
    char comment;
    const char* address;
    const char* maxaddress;
    unsigned int position;
public:
    Parser(const char* _address, const char* _maxaddress) : errorcode(NO_ERROR_PRESENT) {};
    const char* s_address(const char* _address) {address = _address;}
    const char* s_maxaddress(const char* _maxaddress) {maxaddress = _maxaddress;}
    const char* s_position(unsigned int _pos) {position = _pos;}
    char r_comment() const {return comment;}
    const char* r_address() const {return address + position;}
    const char* r_maxaddress() const {return maxaddress;}
    unsigned int r_position() const {return position;}

    int geterror();
    void set_error(int code) {errorcode = code;}

    void set_comment(const char char_comment);
    void set_position(unsigned int position);
    void resetboundary(unsigned int address, unsigned int maxaddress);
};

class Integer: public Parser {
public:
    //Get an int token
    int GetInt();
};

class String: public Parser {
private:
    int NullByToken(char*, int, char);                          //Null a string by token
    void CleanString(std::string string);                       //Clean an string to its simple form (removing spaces, tabs, etc)
    public:
        Displacement* GetEndOfLine();                           //Get len till end of line
        Displacement* GetSimpleString();
        bool SplitByChar(const char token, SplitString* setstrings);
};

class MultiOperation: public String, public Integer {
    void* GetDataByPrefix(unsigned int Type, char token, const char* prefixcmp);
};

函数GETDATABORYPROFIX需要对解析器类的访问,需要对String和整数类的访问。

< P>这是一个已知的钻石层次结构问题,它仅存在于C++中。虽然实现多个接口是可以的,但是应该避免类的多重继承。这就是为什么像c#和java这样的现代语言不允许多重继承

至于你们的课程,我看不出有任何is-a关系。您应该使用has-a(引用而不是插入)

调用您的问题


有几种解决方案,其中之一,你可能会发现很有趣。我建议你用谷歌搜索一下。

你能举个例子吗?使用“Parser has a String”听起来在逻辑上是正确的,但String类需要访问Parser,就像Integer类一样,composition不允许我这样做。谢谢,我会看一看