I appolagise for the slightly ambiguous title, I didn't know what to call it.

I want to create a way to define interfaces without having to also write the class declaration that implements it.
I have the class members, and the interface defined, but I am not sure how (if possible) to make it generate the implentation class declaration as well.

For anyone who doesn't know __interface makes all the functions within it automatically public and pure virtual.

Idealy this is how I would like to write the interfaces:

BeginMembers( ITest )
    int m_nTest;
EndMembers( ITest )

BeginInterface( ITest, CTest ) // not sure if these will be the actuall paramaters
    void TestFunc( void );
EndInterface( ITest, CTest )

void CTest::TestFunc( void )
{
    printf( "%d\n", m_nTest );
}

Which would generate code like this:

class ITest_members
{
    protected:
        int m_nTest;
};

__interface ITest // I'm aware that this is not portable.
{
    public:
        void TestFunc( void );
};

class CTest : public ITest_members, public ITest
{
    public:
        void TestFunc( void );
};

Right now this is the code I have:

#define BeginMembers( iName ) class iName##_members { protected:
#define EndMembers( iName ) };

#define BeginInterface( iName ) __interface iName##_interface {
#define EndInterface( iName ) \
class iName : public iName##_interface, public iName##_members \
{ \
public: \
    ~iName( void ) { } \
};

I'm stuck on how to get it to generate the CTest class declaration, any ideas would be greatly appreciated.
The reason that I'm doing this is that I'm lazy and don't want to have to write the CTest class that is exactly the same as the interface class.

Edited 4 Years Ago by tomtetlaw

I want to create a way to define interfaces without having to also write the class declaration that implements it.

Are you reffering to those classes from OOP implemented virtual?

For anyone who doesn't know __interface makes all the functions within it automatically public and pure virtual.

Indeed, it does. And your question is?

Idealy this is how I would like to write the interfaces:

So, are you trying to figure out what those virtual classes from your interface are for, or are you trying to implement several interfaces through polymorphism?

The reason that I'm doing this is that I'm lazy and don't want to have to write the CTest class that is exactly the same as the interface class.

I'm slightly thinking that you do want the polymorphism implementation.

Edited 4 Years Ago by Lucaci Andrew

Q1. I don't fully understand this question. What classes?
Q2. That part isn't a question.
Q3. I'm trying to write macros that create the definition of the interface class and the definition of the implementation class with me only having to write one.

BE more explicit with your problem, that's what i'm trying to say.
So, Q1: I'm talking about the OOP concept of interfaces.
Q2: rethorical one.
Q3: You want some clases or macros from which you could use them multiple times, on different OS, and on different projects?

Edited 4 Years Ago by Lucaci Andrew

Sorry, I'm not very good at describing problems.

Yeah I want to use OOP interfaces.

It has nothing to do with code reuse for different projects or anything. I just want to write one class definition and have the macros generate both the interface for that class and the class that implements it.

I am using polymorphism yes, I can write the code like this:

class Interface { public: virtual void Foo( void ) = 0; }
class Derived : public Interface { void Foo( void ) { printf( "Foo!\n" ); }

But because I'm lazy, I want to create a number of macros so that I only have to write this code:

BeginInterface( Interface )
    void Foo( void );
EndInterface( Interface, Derived )

And the macros will generate code like in the first sample.
Then I can just write:

void Derived::Foo( void )
{
    printf( "Foo!\n" );
}

After I thought of this idea for a while I was pretty convinced that it was impossible but I though I'd try here to see if anyone has seen or implemented something like this before.

Maybe you are reffering to some kind of templates for these interfaces. Unfortunately, it's not my strongest point, so I'd better let someone more experienced in this domain to handle the situation. I'm sorry for not being able to help you properly.

This article has been dead for over six months. Start a new discussion instead.