473,320 Members | 2,193 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,320 software developers and data experts.

Change C functions to C++ static methods for organisation?

Hi, I've inherited a bunch of C code that needs to be called from
a C++ framework, so I thought it would be good to put these C
functions into C++ classes for better organisation. What are
best practices?

Should I put related C functions into a class and make them
static methods? Most of the C functions are in separate files
with no global variables (one file of functions for producing
random numbers, one file of functions for doing stats analysis,
etc.), so should I make a class out of each of these files and just
make them static functions?

Nov 29 '06 #1
5 1788
On 2006-11-29 07:27, Digital Puer wrote:
Hi, I've inherited a bunch of C code that needs to be called from
a C++ framework, so I thought it would be good to put these C
functions into C++ classes for better organisation. What are
best practices?

Should I put related C functions into a class and make them
static methods? Most of the C functions are in separate files
with no global variables (one file of functions for producing
random numbers, one file of functions for doing stats analysis,
etc.), so should I make a class out of each of these files and just
make them static functions?
That sounds like a Java-solution to me (not necessarily bad) but C++
does nor require everything to be a class, and for many things I think
this makes sense. I'd put the functions in one or more namespaces.

--
Erik Wikström
Nov 29 '06 #2

Digital Puer wrote:
Hi, I've inherited a bunch of C code that needs to be called from
a C++ framework, so I thought it would be good to put these C
functions into C++ classes for better organisation. What are
best practices?

Should I put related C functions into a class and make them
static methods? Most of the C functions are in separate files
with no global variables (one file of functions for producing
random numbers, one file of functions for doing stats analysis,
etc.), so should I make a class out of each of these files and just
make them static functions?
No. You should leave them be. Put them in a namespace if you want but
really...just let them be. If you are going to bother OOing them then
do it for real. Create classes that contain encapsulated data and put
functions that need access to the private data parts in the class and
leave the rest as external functions.

Nov 29 '06 #3
Digital Puer:
Hi, I've inherited a bunch of C code that needs to be called from
a C++ framework, so I thought it would be good to put these C
functions into C++ classes for better organisation. What are
best practices?

Should I put related C functions into a class and make them
static methods? Most of the C functions are in separate files
with no global variables (one file of functions for producing
random numbers, one file of functions for doing stats analysis,
etc.), so should I make a class out of each of these files and just
make them static functions?

The only reason I've ever had to put a perfectly good set of a functions into
a class as static functions, was to aid me in template trickery:

template<class T>
void Func()
{
T::SomeFunc();
}

It seems that a lot of novices are doing nowadays just so they can say
they're code is OO.

--

Frederick Gotham
Nov 29 '06 #4

Noah Roberts wrote:
Digital Puer wrote:
Hi, I've inherited a bunch of C code that needs to be called from
a C++ framework, so I thought it would be good to put these C
functions into C++ classes for better organisation. What are
best practices?

Should I put related C functions into a class and make them
static methods? Most of the C functions are in separate files
with no global variables (one file of functions for producing
random numbers, one file of functions for doing stats analysis,
etc.), so should I make a class out of each of these files and just
make them static functions?

No. You should leave them be. Put them in a namespace if you want but
really...just let them be. If you are going to bother OOing them then
do it for real. Create classes that contain encapsulated data and put
functions that need access to the private data parts in the class and
leave the rest as external functions.
If I use a namespace, the syntax for a function would be the same
as if the function were a static method, right?

For example, MyUtils::doSomething().

There does not seem to be any advantage of a namespace over the
use of static methods to collect together unrelated functions.

Nov 29 '06 #5
On 2006-11-29 22:13, Digital Puer wrote:
Noah Roberts wrote:
>Digital Puer wrote:
Hi, I've inherited a bunch of C code that needs to be called from
a C++ framework, so I thought it would be good to put these C
functions into C++ classes for better organisation. What are
best practices?

Should I put related C functions into a class and make them
static methods? Most of the C functions are in separate files
with no global variables (one file of functions for producing
random numbers, one file of functions for doing stats analysis,
etc.), so should I make a class out of each of these files and just
make them static functions?

No. You should leave them be. Put them in a namespace if you want but
really...just let them be. If you are going to bother OOing them then
do it for real. Create classes that contain encapsulated data and put
functions that need access to the private data parts in the class and
leave the rest as external functions.

If I use a namespace, the syntax for a function would be the same
as if the function were a static method, right?

For example, MyUtils::doSomething().

There does not seem to be any advantage of a namespace over the
use of static methods to collect together unrelated functions.
Well, there's one class less in the application. In the future someone
might come across the code and try to understand what the purpose of the
class is, or even worse, try to instantiate it. Can you come up with any
good reason why you need a class that does nothing except provides a
namespace for the functions? Look at it in another way, is there any
advantage of a class over a namespace?

Using the namespace is probably more OO than using the class, since the
functions are probably to diverse to be in the same class, which would
mean that you should put them in several different classes, so that the
classes are small coherent units. Should the functions on the other hand
form a coherent set of operations they should probably be non-static
methods of some already existing class.

--
Erik Wikström
Nov 29 '06 #6

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

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
2
by: john smith | last post by:
I'm wondering if it's possible to declare a pure virtual member function? Ie is: class A{ public: virtual static void f() const = 0; }; legal? I'm getting compile errors for code that used...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
0
by: MS | last post by:
Is the schema used to validate XML Documentation Comments available to be changed by a VS.Net user? I suspect the this question is no so have a couple of requests: 1.) I use #region...
4
by: MPF | last post by:
When designing a n-tier architecture, what is the preferred method/function accessibility? <Specifically for asp.net apps> A private constructor and shared/static methods & functions? A public...
10
by: David P. Donahue | last post by:
When I wrote websites in VB .NET, I would often put functions in Global for all the pages to call. Now, in C#, doing so results in "references to non-static objects" and whatnot. I realize what...
6
by: Don Wash | last post by:
Hi All! I'm developing ASP.NET pages using VB.NET language. My background is VB6 and ASP3. Right now, I'm evaluating strategies on creating reusable and common functions and methods for ASP.NET....
13
by: Mark | last post by:
are there functions in c# for just members of a class myclass.Method/function Thanks Mark
2
by: Ahmed | last post by:
Ok i understand the fact that once events are raised they are executed on another thread and have no access to member functions or variables because they are static functions. But i am having...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.