Click to See Complete Forum and Search --> : use of Purely Abstract Classes


AdamDH
July 12th, 2006, 08:54 AM
When a purely abstract class is used as an interface in C++ (all member funtions are pure virtual and no data members and no implementation), can this pure abstract class only be used by clients to communicate with the concrete class derived from the pure abstract class?

If the same concrete class needs to communicate via this interface to another interface, can the concrete class's interface be purely abstract anymore? When this occurs a reference to the other interface class is required as well as some implementation to facilitate the communication.

Or should the concrete class use another seperate interface to communicate to other interfaces?

I guess I am confused as to how to implement bidirecional communication via abstract interfaces. This is all possible with abstract class interfaces, but what's the proper use of purely abstract interfaces?

Thanks,
Adam

wildfrog
July 12th, 2006, 09:11 AM
If the same concrete class needs to communicate via this interface to another interface, can the concrete class's interface be purely abstract anymore? When this occurs a reference to the other interface class is required as well as some implementation to facilitate the communication.Not sure what you're trying to say here... can you please elaborate?


#include <iostream>
using namespace std;

// pure virtual interface
class PureVirtual
{
public:
virtual void SomeFunction(int i) = 0;
};


// one implementation
class Receiver : public PureVirtual
{
public:
void SomeFunction(int i)
{
cout << "Receiver is receiving " << i << endl;
}
};


// another one
class Sender : public PureVirtual
{
protected:
PureVirtual *m_ptr; // to whom do we 'send'

public:
Sender(PureVirtual* ptr) : m_ptr(ptr) { }

void SomeFunction(int i)
{
cout << "Sender is sending " << i << endl;
m_ptr->SomeFunction(i);
}
};

int _tmain(int argc, _TCHAR* argv[])
{
Receiver r;
Sender s(&r);
s.SomeFunction(123);
return 0;
}

- petter

Graham
July 12th, 2006, 12:29 PM
This FAQ (http://www.codeguru.com/forum/showthread.php?t=383253) might give you some clues.

AdamDH
July 12th, 2006, 01:24 PM
Thanks for the replies WildFrog and Graham.

Reading my question again, I think I confused myself.

Say I have a concrete class A derived from a pure virtual <<interface>> class AInterface. Say I have a concrete class B derived from a pure virtual <<interface>> class BInterface.

If class A needs to talk to class B and class B needs to talk to class A, class A must maintain a reference to a BInterface and class B must maintain a reference to an AInterface. The references to the interfaces are held by the concrete classes not their parent class interfaces. This, I think is where I was confused.

The interfaces are purely abstract (contain only pure virtual functions) and the concrete classes maintain the references to the interfaces it needs to communicate with. Right?

Thanks again,
Adam

Graham
July 12th, 2006, 02:28 PM
Yep.