By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

optional interface

P: n/a
How do I write a class with "optional" interface like the following one:

class A
{
:
// inteface_a
// interface_b
};

void main()
{
A obj_a(interface_a); // define an object with interface a
A obj_b(interface_a | interface_b); // define an object with
interface a AND b

obj_b.func_b(); // calling function defined in interface
b; OK
obj_a.func_b(); // calling function not defined in
interface a; Compilation error
}

--

Can I write something like this in C++? Thanks.
Regards,
Milan.
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Milan" <so*****@internet.com> wrote...
How do I write a class with "optional" interface like the following one:

class A
{
:
// inteface_a
// interface_b
};

void main()
int main()
{
A obj_a(interface_a); // define an object with interface a
A obj_b(interface_a | interface_b); // define an object with
interface a AND b

obj_b.func_b(); // calling function defined in interface b; OK
obj_a.func_b(); // calling function not defined in
interface a; Compilation error
}

--

Can I write something like this in C++? Thanks.


By using templates, I suppose. Something like

template<class a, class b = a> class A : public a, public b
{
};

// partial specialisation
template<class a> class A<a,a> : public a
{
};

struct interface_a
{
void func_a();
};

struct interface_b
{
void func_b();
};

int main()
{
A<interface_a> a_a;
A<interface_a, interface_b> a_ab;
a_a.func_a(); // OK
a_a.func_b(); // error
a_ab.func_b(); // OK
}

Victor
Jul 19 '05 #2

P: n/a
"Milan" <so*****@internet.com> wrote in message
news:bg*********@imsp212.netvigator.com...
How do I write a class with "optional" interface like the following one:

class A
{
:
// inteface_a
// interface_b
};

void main()
int main() according to the standard
{
A obj_a(interface_a); // define an object with interface a
A obj_b(interface_a | interface_b); // define an object with
interface a AND b

obj_b.func_b(); // calling function defined in interface
b; OK
obj_a.func_b(); // calling function not defined in
interface a; Compilation error
}

--

Can I write something like this in C++? Thanks.
Regards,
Milan.


Inheritance looks suitable.

class A
{
public:
void func_a();
virtual ~A();
};

class B : public A
{
public:
void func_b();
};

A obj_a;
B obj_b;

obj_a.func_b(); // illegal
obj_b.func_b();
obj_b.func_a(); // legal

--
ES Kim

Jul 19 '05 #3

P: n/a
"Milan" <so*****@internet.com> wrote in message news:<bg*********@imsp212.netvigator.com>...
[how do I? question]

Sure, use inheritance. Put the stuff that is common to all
types in the base. Put the specialized stuff in different
child types.

For extra credit, look up public, private, and protected
inheritance.
Socks
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.