473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

C++ Design question

Hi All,

I'm making a class which derives(implements) from 5 interfaces:

class myClass: public base1, public base2, public base3,.......

I want to be able to ask from any interface about a pointer to another
interface
What I mean is:

myClass _myImpObj;

base1 *b1 = &_myImpObj;
base *b2 = b1->QueryInterafce("b2");
base *b3 = b2->QueryInterafce("b3");
base *b1N = b3->QueryInterface("b1");

Its is very simialir to COM mechanism.Anyone knows how to implement it?

Thanks,
Denis

Jan 14 '07 #1
4 1453
void* myClass::QueryInterface(char* str)
{
if (!strcmp(str, "b1"))
return (base1*)this;
....
....
}

Is this what you meant?
denis wrote:
Hi All,

I'm making a class which derives(implements) from 5 interfaces:

class myClass: public base1, public base2, public base3,.......

I want to be able to ask from any interface about a pointer to another
interface
What I mean is:

myClass _myImpObj;

base1 *b1 = &_myImpObj;
base *b2 = b1->QueryInterafce("b2");
base *b3 = b2->QueryInterafce("b3");
base *b1N = b3->QueryInterface("b1");

Its is very simialir to COM mechanism.Anyone knows how to implement it?

Thanks,
Denis
Jan 14 '07 #2

denis wrote:
Hi All,

I'm making a class which derives(implements) from 5 interfaces:

class myClass: public base1, public base2, public base3,.......

I want to be able to ask from any interface about a pointer to another
interface
What I mean is:

myClass _myImpObj;

base1 *b1 = &_myImpObj;
base *b2 = b1->QueryInterafce("b2");
base *b3 = b2->QueryInterafce("b3");
base *b1N = b3->QueryInterface("b1");

Its is very simialir to COM mechanism.Anyone knows how to implement it?
You can use dynamic_cast:

#include <iostream>

template <int N>
struct base{
virtual ~base(){}
};

struct my12 : base<1>, base<2>{};

struct my34 : base<3>, base<4>{};

struct my5 : base<5>{};

struct my12345 : base<1>,base<2>, my34 ,my5{};

template <int N, typename T>
bool has_interface(T * t)
{
bool is = dynamic_cast< base<N* (t) !=0;
std::cout << typeid(T).name() << " "
<< ((is)?"Is":"Is not")<< " a base<" << N << ">\n";
return is;
}

template <int N>
struct interfaces;

template <>
struct interfaces<1>{
template <typename T>
void operator()( T * t)const
{
has_interface<1>(t);
}
};

template <int N>
struct interfaces{
template <typename T>
void operator()( T * t)const
{
interfaces<N-1f;
f(t);
has_interface<N>(t);
}
};

int main()
{
//test 5 interfaces
interfaces<5test;

//test some expressions
my12 var12;
test(&var12);

std::cout <<"---------\n\n";

my34 var34;
test(&var34);

std::cout <<"---------\n\n";

my5 var5;
test(&var5);

std::cout <<"---------\n\n";

my12345 var12345;
test(&var12345);

}

regards
Andy Little

Jan 14 '07 #3
kwikius wrote:

//forgot crosscast test:

/*
....
*/
std::cout <<"---------\n\n";

my12345 var12345;
test(&var12345);

std::cout <<"---------\n\n";

//crosscast test
base<1>* bb = &var12345;

test(bb);
>
}
regards
Andy Little

Jan 14 '07 #4
On 14 Jan 2007 01:42:35 -0800, denis <de**********@hotmail.comwrote:
Hi All,

I'm making a class which derives(implements) from 5 interfaces:

class myClass: public base1, public base2, public base3,.......

I want to be able to ask from any interface about a pointer to another
interface
What I mean is:

myClass _myImpObj;

base1 *b1 = &_myImpObj;
base *b2 = b1->QueryInterafce("b2");
base *b3 = b2->QueryInterafce("b3");
base *b1N = b3->QueryInterface("b1");

Its is very simialir to COM mechanism.Anyone knows how to implement it?
Not knowing what you want to accomplish, this sounds like a really bad idea.
Both the code itself, and the wish to emulate COM.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Jan 19 '07 #5

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

Similar topics

5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
2
by: Test User | last post by:
Hi all, (please excuse the crosspost as I'm trying to reach as many people as possible) I am somewhat familiar with Access 2000, but my latest project has me stumped. So, I defer to you...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
17
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.