virtual function and pure virtual function in the implementation of COM

I

IK

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
 
V

Victor Bazarov

IK said:
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
 
H

hemraj

IK said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top