Classes interaction advice

Hi everyone!

I have a littl conception issue.

For my little chat client in C++ I'd like to clarly separate the interface part (input and output) from the XMPP core manager.

For the little story, I use ncurse for the interface (I haven't find any good workaround in C++) and gloox for the XMPP part.

Here is what I'd like to do:

INPUT -------> PARSER ------> OUTPUT

and PARSER <------> CORE

I must add that the arrows don't describe an inheritance relation between features.

The PARSER translate the commands from the user (incoming from INPUT) and throw actions (among others from the CORE) and prints the results on the OUTPUT.
For instance, when I will strike /lis_contacts on INPUT, the PARSER will be in charge to recognize the command and launch a member function of CORE to return the list of contact to OUTPUT.

The fact is that I don't want PARSER to be completely dependent of one implementation of CORE, eg I don't want to have a PARSER member in my CORE class.

To solve this issue I though about making my CORE class a child of the PARSER class. I should then redefine member function of PARSER in my CORE class. I would then not be dependant of a unique implementation of the PARSER class.
It is just weird to make one of my class by a child of the other although I don't really see them in a hierarchy relation.

One has also propose me to use function pointer. However, in all cases, declaring a function pointer on a member function of a class is almost equal to link very closely my class to my pointer from another class...

Maybe am I too much demanding concerning this independence between my classes et I must at a moment provide the first class with information about the other.

What do you think of all this?

Thanks!
I think you need an interface. The parser receives a pointer to an object that implements a specific interface. This interface can be implemented by multiple version of CORE. The concept of interface is just a pure virtual class, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class ICore //Interface names usually start with I.
{
public:
    //Required.
    virtual ~ICore() { }

    //Now the interface methods.
    virtual void ReceiveInpunt(const wchar_t * const szInput) = 0;
    virtual std::wstring Output() const = 0;
};

//Now you can create multiple implementations of CORE as long as they inherit from ICore.
//Then PARSER will be able to use any of the cores without modification.

class Parser
{
private:
    ICore *_pCore;

public:
    Parser(ICore *pCore) : _pCore(pCore) { }

    Parse(const wchar_t * const szInputString)
    {
        wchar_t *standardizedInput;
        //Convert raw input string to useful input string according to parser's logic.
        //Then pass the standardized input to the core.
        pCore->ReceiveInput(standardizedInput);
    }
    std::wstring Output()
    {
        return pCore->Output();
    }
};


Good enough?
sorry for my lack of knowledge , but i dont get it ...as i see that you are not inheriting the class ICore but encapsulating it . HOw can it solve the problem at hand ?
The class ICore will be the ancestor for CORE classes. The OP said he/she did not want to be forced to a single implementation of CORE. By using interfaces you can plug into any implementation as long as the implementations follow the rule of inheriting from ICore. Like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class MyCoreImp : public ICore
{
private:
    std::wstring _theOutput;

public:
    void ReceiveInput(const wchar_t * const szInput)
    {
        //Process the input and get the output ready.
        _theOutput = <the result of whatever processing was needed>;
    }
    std::wstring Output() const
    {
        return _theOutput;
    }
};

class MyCoreImp2 : public ICore
{
private:
    std::wstring _theInput;

public:
    void ReceiveInput(const wchar_t * const szInput)
    {
        //This one works differently:  It stores the input only.
        _theInput = szInput;
    }
    std::wstring Output() const
    {
        //First process the input, then return the output:
        return std::wstring(<the process to calculate output>);
    }
};

//So now you have two cores that you can use with a single parser.

int main()
{
    MyCoreImp core1;
    MyCoreImp2 core2;
    Parser myParser1(&core2);
    std::wstring myInput;
    std::getline(std::wcin, myInput);
    myParser1.Parse(myInput.c_str());
    //Or
    Parser myParser2(&core1);
    myParser2.Parse(myInput.c_str());
    //Output:
    std::wcout << myParser2.Output() << std::endl;
    std::wcout << myParser1.Output() << std::endl;
    return 0;
}
Fine, thanks!

That's exactly what I was looking for. In fact I read this solution in Bjarne S's book but I didn't really know how to adapt it to my issue.
To bluecoder: thanks to pointer and polymorphism I can send to parser a child of ICore and the member function called should be the one of the child....

It looks good anyway, I will try it asap!

HI,

I'm back for another rookie question!
Let's assume that I want to create 2 very intimate classes (A and B).
They would like to send and/or receive signal from each other.
My first idea was to declare a member object (or pointer or reference) of A into B and vice versa.
At this point, how to declare and define the first without having already declared the other? I'm not sure it's possible...

My second idea was to create an intermediate class C that would have member objects of A and B type. So, from a C object I could make communicate an object of A and B.
What do you think of this?

Maybe there's a very efficient and mere method that I didn't catch in all my C++ books ;)
If you know it let me know asap :D

Thanks!

thanks webJose ..for clearing the point...I got it now ..
Topic archived. No new replies allowed.