473,795 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1809
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::doSome thing().

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...jus t 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::doSome thing().

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
5926
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 important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
2
33180
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 to work before I added the changes in, and I'm not sure if that's causing it.
11
4619
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
0
1451
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 extensively along with the Outlining key functions to organize and navigate my code. In general, having an uncatagorized list of undifferentiated declarations in the generated HTML documentation is not all that helpful. Please have VS.Net do one of the...
4
1962
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 constructor and non-shared/static methods & functions? Are there any drawbacks with regards to performance with either model? Thanks,
10
6772
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 that means and all, but what I'm wondering is what's the best way around it? Say, for example, I want a function that takes a username and a password and returns true or false if it's a successful login, and I want any page or usercontrol in the...
6
1675
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. In ASP 3, it was nothing more than functions and methods coded in #Include Files. Now ASP.NET offers features such as Page Inheritance and full software development support. So I'm just wondering, what should I do if I have 5 functions, that...
13
2303
by: Mark | last post by:
are there functions in c# for just members of a class myclass.Method/function Thanks Mark
2
1895
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 trouble how i can Invoke these methods from the static method. Can anyone help me out here? :) TIA!
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10448
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.