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

Library I/F design policy

P: n/a
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
Jun 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
And should I make the new operators members or non-members?

Thanks -

Paul
Jun 14 '07 #2

P: n/a
On Jun 14, 6:25 pm, Paul French <pf9...@yahoo.comwrote:
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

Jun 14 '07 #3

P: n/a
On Jun 14, 7:36 pm, Amal P <enjoyam...@gmail.comwrote:
On Jun 14, 6:25 pm, Paul French <pf9...@yahoo.comwrote:
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
You can overload operator as either member or friends.

Jun 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.