I have a problem with some concepts of OOP class, lets say i have the following classes:
- Parser class which includes data for parsing an stream data
- String class that include string operations for parse the stream data
- Integer class that include integer operations for parse the stream data
So String and Integer class inherit the Parser class since they both need specific info regarding the stream like the position, the length, etc.
Now the problem comes when i have an function that uses both String and Integer functions.
Lets put this new function in a class called MultipleOperations. MultipleOperations needs the String and Integer class so it inherit both but String and Integer class already inherit Parser, so when trying to access some data from Parser class is ambiguous.
In the other hand if set String and Integer class has a composition of MultipleOperations then i wont have access to Parser class.
Also i don’t understand much the concept of “has a” since in most cases i need to reference data from the base class, so that makes it a “is a”.
Here is an example of my problem:
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);
};
Function GetDataByPrefix needs access to Parser class and needs access to both String and Integer class.
Your problem is called the diamond problem.
There are several solutions for this, to mention one, you may find it interesting. I suggest you to google it a bit.