473,466 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

virtual function and pure virtual function in the implementation of COM

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

--
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/
Jul 22 '05 #1
2 1697
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
Jul 22 '05 #2
IK <pr*********@yahoo.com> wrote in message news:<20*******************@foorum.com>...
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.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Ele | last post by:
Is it correct to say that Whenever a class has a virtual member function, define its destructor as "virtual"? Can a destructor as "pure virtual"? When is it needed to do so? For an interface,...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
10
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
2
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
3
by: keith | last post by:
Dear mentors and gurus, I noticed at the end of section 22.4 of the 'FAQ-Lite' it says "Note that it is possible to provide a definition for a pure virtual function, but this usually confuses...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.