CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2006
    Posts
    31

    use of Purely Abstract Classes

    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

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: use of Purely Abstract Classes

    Quote Originally Posted by AdamDH
    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?

    Code:
    #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

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: use of Purely Abstract Classes

    This FAQ might give you some clues.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Jun 2006
    Posts
    31

    Re: use of Purely Abstract Classes

    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

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: use of Purely Abstract Classes

    Yep.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured