Library I/F design policy

P

Paul French

I've got an old C arithmetic library that I want to convert to C++.
There are about 30 routines, which all take pointers to an arithmetic
structure.

I'd like to convert this to C++, so that the user sees only a new
arithmetic class, with overloaded arithmetic ops (+/-/etc) which call
the old C code under the hood. The user doesn't need to see the old C
code. The new class contains only a few fields, and the user doesn't
need to see these either.

The problem is, I can see 2 or 3 ways to do this, and it's starting to
get messy. Any advice on how to do this? I'm currently using
combinations of these 3:

1 - The C functions are all private member methods, with direct access
to the new fields. This is the easiest, but may have long-term
maintenance problems. The library user also finds out about the
existence of the C routines, since they're declared in the class
header. This is not really a problem, but I'd prefer the header to be
clean.

2 - The C functions are all external, in their own namespace, and the
class declares them as friends, so they can still see the class
internals. Again, perhaps not good in the long term.

3 - The C functions are again all external, in their own namespace. I
now have access methods for all the private class fields, and the C++
and C code is modified to use these access methods. This is messy, but
I suppose it's the Right Thing to do.

?

Thanks -

Paul
 
A

Amal P

And should I make the new operators members or non-members?

Thanks -

Paul

Dear Paul,

I will suggest you a different method.
You can make a call which contains just API's.
And another class will have calls to C functions corresponding to
each API.
So you will have a class ArithmaticAPI and a class
ArithmaticManager.
ArithmaticManager's object is created in ArithmaticAPI and the user
only need to know about the class ArithmaticAPI.
You can either make the C language code as a private member of
ArithmaticManager or you can make those function's friends.

eg:

ArithmaticManager.h

class ArithmaticManager
{
public:
int Greatest( int, int );
int Smallest( int, int );
};

ArithmaticAPI.h

#include ArithmaticManager.h
class ArithmaticAPI
{
private:
ArithmaticManager m_ArithmaticMgr;
public:
int Greatest( int a, int b ){ return
m_ArithmaticMgr.Greatest(); }
int Smallest( int a, int b ){ return
m_ArithmaticMgr.Smallest(); }
}


Even the member of ArithmaticManager need not have to be created
provided you declare ArithmaticManager functions as static ones.

Thanks and regards,
Amal P
 
A

Amal P

Dear Paul,

I will suggest you a different method.
You can make a call which contains just API's.
And another class will have calls to C functions corresponding to
each API.
So you will have a class ArithmaticAPI and a class
ArithmaticManager.
ArithmaticManager's object is created in ArithmaticAPI and the user
only need to know about the class ArithmaticAPI.
You can either make the C language code as a private member of
ArithmaticManager or you can make those function's friends.

eg:

ArithmaticManager.h

class ArithmaticManager
{
public:
int Greatest( int, int );
int Smallest( int, int );
};

ArithmaticAPI.h

#include ArithmaticManager.h
class ArithmaticAPI
{
private:
ArithmaticManager m_ArithmaticMgr;
public:
int Greatest( int a, int b ){ return
m_ArithmaticMgr.Greatest(); }
int Smallest( int a, int b ){ return
m_ArithmaticMgr.Smallest(); }
}

Even the member of ArithmaticManager need not have to be created
provided you declare ArithmaticManager functions as static ones.

Thanks and regards,
Amal P

You can overload operator as either member or friends.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top