virtual function and pure virtual function in the implementation of COM

Discussion in 'C++' started by IK, Jul 21, 2004.

  1. IK

    IK Guest

    Hello All,
    Please excuse me for posting this here, but I don't find any other group where I
    will get a proper answer. This is about clarifying the C++ part of COM.

    I understand that COM is a mechanism by which interface and implementation is
    seperated. A basic com implementation in c++ can be as follows.


    // Dev-CPP
    // interface
    // Ai.h
    class Ai {
    public:
    virtual void foo() = 0;
    virtual Ai* GetInterface() =0;
    };



    // Implementation
    // A.h
    #include "Ai.h"
    class AImpl : public Ai {
    public:
    void foo();
    Ai* GetInterface();
    };

    // A.cpp
    #include "A.h"

    AImpl::foo()
    {
    }

    Ai* AImpl::GetInterface()
    {
    Aimpl* imp = new Ai();
    return (Ai*) imp;
    }

    // vc++
    // Code using the interface

    #include "Ai.h"


    int main()
    {
    // Ai* a = NULL;

    // Load the dll
    // Get the address to the function GetInteface

    // a = <address>->GetInterface();
    // a->foo()

    }

    This is the principle of COM. I couldnt make out why in the interface it should
    be "pure" virtual function ? Why cant it be simple virtual function like.
    virtual void foo(); Will it really matters, since the examples I have written,
    doesnt have any problem while compiling and running. The test I did is that I
    did the implementation on Dev-Cpp which uses MingW, then I called this DLL from
    VC++ , to know whether any name mangling issues creates any problem. Can
    somebody please help me to understand it better ?

    Thanks and Regards
    Ik

    --
    Use our news server 'news.foorum.com' from anywhere.
    More details at: http://nnrpinfo.go.foorum.com/
    IK, Jul 21, 2004
    #1
    1. Advertising

  2. Re: virtual function and pure virtual function in the implementationof COM

    IK wrote:
    > Hello All,
    > Please excuse me for posting this here, but I don't find any other group where I
    > will get a proper answer. This is about clarifying the C++ part of COM.
    >
    > I understand that COM is a mechanism by which interface and implementation is
    > seperated. A basic com implementation in c++ can be as follows.
    >
    >
    > // Dev-CPP
    > // interface
    > // Ai.h
    > class Ai {
    > public:
    > virtual void foo() = 0;
    > virtual Ai* GetInterface() =0;
    > };
    >
    >
    >
    > // Implementation
    > // A.h
    > #include "Ai.h"
    > class AImpl : public Ai {
    > public:
    > void foo();
    > Ai* GetInterface();
    > };
    >
    > // A.cpp
    > #include "A.h"
    >
    > AImpl::foo()
    > {
    > }
    >
    > Ai* AImpl::GetInterface()
    > {
    > Aimpl* imp = new Ai();
    > return (Ai*) imp;
    > }
    >
    > // vc++
    > // Code using the interface
    >
    > #include "Ai.h"
    >
    >
    > int main()
    > {
    > // Ai* a = NULL;
    >
    > // Load the dll
    > // Get the address to the function GetInteface
    >
    > // a = <address>->GetInterface();
    > // a->foo()


    I wonder why you commented all that out... Doesn't compile if you don't?
    What's the point of having the actual program that is supposed to do
    something written in comments?

    >
    > }
    >
    > This is the principle of COM. I couldnt make out why in the interface it should
    > be "pure" virtual function ? Why cant it be simple virtual function like.
    > virtual void foo(); Will it really matters, since the examples I have written,
    > doesnt have any problem while compiling and running. The test I did is that I
    > did the implementation on Dev-Cpp which uses MingW, then I called this DLL from
    > VC++ , to know whether any name mangling issues creates any problem. Can
    > somebody please help me to understand it better ?


    In terms of C++ it really doesn't matter for the calls made to 'foo'.
    What matters here is that you cannot instantiate 'Ai'. Any time
    a class has pure virtual functions, it cannot be instantiated. That
    is the requirement of the language.

    I am not going to guess what COM implementers had in mind, you should
    probably ask in a COM newsgroup, perhaps they wanted the users of each
    and every class to retrieve the interface from the server (instead of
    trying to create a copy of it locally). Since DLLs are not part of
    the language specification, any discussion on why it has to be done
    the way it was done falls outside the scope of this newsgroup.

    Dynamic binding is essential for many systems. A complete program (no
    DLLs) can still utilise dynamic binding a polymorphism. The use of
    loadable modules takes it to the next level, where the implementations
    can be totally unknown at the time the program is written. However,
    it does involve some mechanisms that are not defined by C++ itself.

    Victor
    Victor Bazarov, Jul 23, 2004
    #2
    1. Advertising

  3. IK

    hemraj Guest

    IK <> wrote in message news:<>...
    > Hello All,
    > Please excuse me for posting this here, but I don't find any other group where I
    > will get a proper answer. This is about clarifying the C++ part of COM.
    >
    > I understand that COM is a mechanism by which interface and implementation is
    > seperated. A basic com implementation in c++ can be as follows.
    >
    >
    > // Dev-CPP
    > // interface
    > // Ai.h
    > class Ai {
    > public:
    > virtual void foo() = 0;
    > virtual Ai* GetInterface() =0;
    > };
    >
    >
    >
    > // Implementation
    > // A.h
    > #include "Ai.h"
    > class AImpl : public Ai {
    > public:
    > void foo();
    > Ai* GetInterface();
    > };
    >
    > // A.cpp
    > #include "A.h"
    >
    > AImpl::foo()
    > {
    > }
    >
    > Ai* AImpl::GetInterface()
    > {
    > Aimpl* imp = new Ai();
    > return (Ai*) imp;
    > }
    >
    > // vc++
    > // Code using the interface
    >
    > #include "Ai.h"
    >
    >
    > int main()
    > {
    > // Ai* a = NULL;
    >
    > // Load the dll
    > // Get the address to the function GetInteface
    >
    > // a = <address>->GetInterface();
    > // a->foo()
    >
    > }
    >
    > This is the principle of COM. I couldnt make out why in the interface it should
    > be "pure" virtual function ? Why cant it be simple virtual function like.
    > virtual void foo(); Will it really matters, since the examples I have written,
    > doesnt have any problem while compiling and running. The test I did is that I
    > did the implementation on Dev-Cpp which uses MingW, then I called this DLL from
    > VC++ , to know whether any name mangling issues creates any problem. Can
    > somebody please help me to understand it better ?
    >
    > Thanks and Regards
    > Ik


    Hi

    The interface should be abstract interface. And ideally should contain
    all pure virtual function and no data member.
    Also in COM we never instantiate interface. We instantiate class which
    implement that interface. client will get pointer to interface which
    is actually pointer to derived class.

    Hope this helps

    Regards
    Hemraj Chaudhari.
    hemraj, Jul 23, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Todd Aspeotis
    Replies:
    3
    Views:
    451
    Kanenas
    May 30, 2005
  2. Replies:
    3
    Views:
    346
    AnalogFile
    Mar 19, 2006
  3. John Goche
    Replies:
    10
    Views:
    725
    Marcus Kwok
    Dec 8, 2006
  4. Replies:
    7
    Views:
    581
    James Kanze
    May 2, 2007
  5. a
    Replies:
    7
    Views:
    356
    dasjotre
    Jun 28, 2007
Loading...

Share This Page