473,889 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

differences between a c-function and a class method

hi all
i have a question:

what is the difference between a c-function and an c++ class method
(both do exactly the same thing).

lets say, i have a function called print2std() and a class called CUtils
with a static method called print2std()

The first one can be called within a programm simply with print2std(),
but the second one must be called with CUtils::print2s td().

what are the differences ?
Or why use c++ programmers sometimes c-functions, too ?
Why don't they use classes with static methods instead?
Regards
gustav

Jul 23 '05 #1
6 2553
Int your case CUtils has not to be a class - usually it is a namespace
(for example you have std namespace, and sqrt(...) functions are placed
there).
---------------------------------
namespace std{
double sqrt(double d) { ... }
}
---------------------------------

If you write your own sqrt function, your function name will not
conflict with std::sqrt().
static methods are a bit more advanced - they can be private or
protected - you can hide them for some reason, so they cannot be used
from outside of the class.

Jul 23 '05 #2


gustav04 wrote:
hi all

i have a question:

what is the difference between a c-function and an c++ class method
(both do exactly the same thing).

lets say, i have a function called print2std() and a class called CUtils
with a static method called print2std()

The first one can be called within a programm simply with print2std(),
but the second one must be called with CUtils::print2s td().

what are the differences ?
Or why use c++ programmers sometimes c-functions, too ?
Why don't they use classes with static methods instead?


The basic difference is that the static class method function can access
other static members of the class, whereas the function outside of a class
may not be able to depending on how the protection is set up.

You may want to use a generic function instead of a class member function if
for example you are writing something not dependent on a data structured
defined as a class, or if you are writing something that works with data
structures for several different classes.

David
Jul 23 '05 #3

"gustav04" <gu******@gmx.a t> wrote in message
news:cv******** **@newsreader1. utanet.at...
hi all
i have a question:

what is the difference between a c-function and an c++ class method (both
do exactly the same thing).

lets say, i have a function called print2std() and a class called CUtils
with a static method called print2std()

The first one can be called within a programm simply with print2std(), but
the second one must be called with CUtils::print2s td().

what are the differences ?
Or why use c++ programmers sometimes c-functions, too ?
Why don't they use classes with static methods instead?
Regards
gustav

IF your C procedure operates on "Class data" then you've messed the whole ++
thing. It's a matter of Object Orientation.
- a C procedure takes up less space than C++;
- a C procedure takes up more Time than C++, unless you are passing a lot of
references to the data being operated on.
- if data flows out of an object and not back in, I'd say use C when you
can. If the data flows back into an object, stick to C++.
- the matter is one of protection, and believe me, you will save on aspirin
debugging large projects.
- objects of the same class share methods, but use a seperate base pointer
to this objects data. a C procedure needs to pass in ALL of the pointers to
data.
- a C procedure doesn't care whos data its messing with, or if the data is
valid.
Jul 23 '05 #4

"DHOLLINGSWORTH 2" <DH************ *@cox.net> wrote in message
news:tdcUd.1860 2$yr.4479@okepr ead05...

"gustav04" <gu******@gmx.a t> wrote in message
news:cv******** **@newsreader1. utanet.at...
hi all
i have a question:

what is the difference between a c-function and an c++ class method (both
do exactly the same thing).

lets say, i have a function called print2std() and a class called CUtils
with a static method called print2std()

The first one can be called within a programm simply with print2std(),
but the second one must be called with CUtils::print2s td().

what are the differences ?
Or why use c++ programmers sometimes c-functions, too ?
Why don't they use classes with static methods instead?
Regards
gustav

IF your C procedure operates on "Class data" then you've messed the whole
++ thing. It's a matter of Object Orientation.
- a C procedure takes up less space than C++;
- a C procedure takes up more Time than C++, unless you are passing a lot
of references to the data being operated on.
- if data flows out of an object and not back in, I'd say use C when you
can. If the data flows back into an object, stick to C++.
- the matter is one of protection, and believe me, you will save on
aspirin debugging large projects.
- objects of the same class share methods, but use a seperate base pointer
to this objects data. a C procedure needs to pass in ALL of the pointers
to data.
- a C procedure doesn't care whos data its messing with, or if the data is
valid.


What??????

Where the heck did you get this information from?

First off, there are no "C" procedures versus "C++" procedures. If you're
compiling in C++, it's ALL C++! The difference is between member functions
and non-member functions, not C and C++. I understand that C++ has added
the use of member functions, but that doesn't make non-member function "C
procedures".

What makes you say that a non-member function takes up less space? Are you
referring to the parameters that get pushed when the function gets called?
In that case, there is indeed (at least conceptually, if not always in fact)
space reserved for the "this" pointer when making the call, but the itself
function is no larger. Also, static member functions, as in the OP's
example, don't use a "this" pointer, and so do not even take up that extra
space when called.

Next (referring to objects and the use of C or C++ based on data flow
direction), what makes you prefer using a non-member function for reading
data but a member function for writing data? That's very inconsistent.
Perhaps you're referring to the somewhat common practice of using a struct
and non-member functions for common tasks that don't require any special
data handling, such as a Point struct, which only has x and y members and is
used simply as a data holder for passing coordinates to functions like Move
and MoveTo? Regardless, it's not possible to only pass data ot of an
object. The data HAS to go in, somehow!

I have no idea what you're trying to say about non-member requiring passing
in ALL of the pointers to the data! You can certainly pass a pointer to a
struct or class to a non-member function. In member funcitons, if you're
operating on the object itself, then you don't have to *explicitly* pass the
object instance (unless you're calling a static member function). But it
*does* get passed to the function, as a kind of "hidden" parameter.

Finally, I don't see what the validity of the data has to do with anything,
regardless of whether you're referring to a member or non-member function.

To the OP:

I'd disregard the above posting altogether, if I were you.

Static member functions are indeed quite similar to non-member functions, in
that no "this" pointer exists, so there is no reference to a "current"
object. But you *can* access static member data or other static member
functions directly from a static member function, so they're not exactly the
same.

As for *why* use non-member functions, the most common reason, I suppose, is
when the function does not need to refer to an object. Consider the sin()
function. It takes an angle in radians and returns the sine for that angle.
No object is needed, and it would be more trouble to create an object just
to be able to call its sin function, wouldn't it?

There are many other common functions, often called "global" functions, that
act as utlities (scanf, sprintf, atoi, etc.) which don't require objects.
You should design your own code in a manner that is safe, consistent,
logical, and maintainable. And to help do that, I'd recommend some good
books, such as Scott Meyer's two "Effective C+" and More Effective C++"
books, and Stroustrup's "The C++ Programming Language".

-Howard


Jul 23 '05 #5
[some typo corrections...]
space reserved for the "this" pointer when making the call, but the itself
function is no larger. Also, static member functions, as in the OP's
should have read: "but the function itself is no larger"
and MoveTo? Regardless, it's not possible to only pass data ot of an
object. The data HAS to go in, somehow!
should have read: "not possible to only pass data OUT of an object"
struct or class to a non-member function. In member funcitons, if you're
"functions"
books, such as Scott Meyer's two "Effective C+" and More Effective C++"


his name is Meyers, so that should be "Meyers'", not "Meyer's".

-Howard
Jul 23 '05 #6
gustav04 wrote:

[ ... ]
lets say, i have a function called print2std() and a class called
CUtils with a static method called print2std()

The first one can be called within a programm simply with
print2std(), but the second one must be called with
CUtils::print2s td().

what are the differences ?
Or why use c++ programmers sometimes c-functions, too ?
Why don't they use classes with static methods instead?


A static member function and a non-member function are generally
equivalent in terms of code produced, calling convention, etc.

As to when/why you'd use a non-member function, I'd turn the question
around: when/why SHOULD you use a static member function instead of a
non-member function? Generally speaking, you should do so when it makes
sense to -- when the function is logically related to one (and only
one) class.

Even that, however, is really a little too broad -- there are times
that a function clearly IS related specifically to one particular
class, and it still can't be a member function (static or otherwise).
One obvious case would be when you're overloading an operator. For
example, consider something like:

class X {
// ...
};

std::ostream &operator<<(std ::ostream &os, X const &x);

This might be a _friend_ of the class, but if you attempt to make it a
member of the class, it simply won't work. To work as a member
function, it would have to be a member of class ostream -- but it would
be impractical to modify class ostream every time we want to support
output for a new type. Worse, making them members of ostream would give
them access to the internals of ostream, and we really don't want that
at all.

The same is true in quite a few other cases of operator overloading.
Consider something like:

class rational {
int num, denom;
public:
rational(long numer=0, long denom=1);
// rational operator+(ratio nal const &);
};

rational operator+(ratio nal const &, rational const &);

Now, if we used the member operator+, it would only work when its left
operand was already a rational number -- something like y=x+2 would
work, but y=2+x would not. Since people normally expect the two to be
equivalent, this is a bad thing.

The cure is to use the non-member overload of operator+. This can do a
conversion on the left operand, so it comes out as something like:

y=operator+(rat ional(2), x);

or:

y.operator=(ope rator+(rational (2), x));

and life is good. Of course, the same is also true when dealing with
most other operators as well -- though a few such as assignment must be
member functions, since it doesn't make much sense to create a
temporary object to assign to.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #7

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

Similar topics

0
1915
by: Dan Gass | last post by:
The difflib.py module and the diff.py tools script in Python 2.4 alpha 3 now support generating side by side (with intra line differences) in HTML format. I have found this useful for performing build release comparisons (I use a script to generate a main page showing all the files that were changed in a build with hyperlinks to side by side differences for each of those files). I also find it useful for generating HTML test reports to...
6
3130
by: Martin Meyer im Hagen | last post by:
Hello, I've got installed Win 2003 SBS Premium with the SQL Server 2000 on a server machine. It works almost fine, except the application which uses the SQL Server. The main part of the application runs (since the last update) fine, but other tools of that application (database import and the database manager for check and rebuild) doesn't. They hang up or kill the database. Our software developer says that these problems are in...
2
8283
by: Daniel | last post by:
Hi, Are there any differences between C# and VB.Net besides syntax? Performance-wise, how do they compare? Thanks, Dan
14
2077
by: Bern | last post by:
what are all the diferences between the two?
2
11854
by: Patrick | last post by:
Are the differences between a search engine, a subject directory and a meta search engine significant for an ebusiness web site owner? A meta search engine merely uses ordinary existing search engines. A subject directory classifies websites into some category. Could anyone point me towards the differences between these three that are significant for an ebusiness web site owner?
13
9647
by: Kieran | last post by:
I am designing a content management system and I want to make sure all pages entered into it's database by users are using valid HTML. I have designed the system to use HTML 4.01 Transitional throughout, however the php powered html checker I have only caters for HTML 4.01 Strict. If possible I need to know the complete differences between Strict and Transitional so I can go through the php script and correctly set it up to properly...
3
3084
by: Daniel | last post by:
Are the differences between MSXML and .Net XSL transformation documented online anywhere? Many of my XSL's work in MSXML but transform differently in ..Net XSL transformation.
4
3719
by: MS | last post by:
Just a general question here re VBA. Can anyone explain the differences between "!" and "." when refering to a control? eg Me!TxtBox and Me.TxtBox. What is difference between "+" and "&" when working with strings. eg MyStr = "I have " & MyNumber & " apples." and MyStr = "I have " + MyNumber + " apples."
15
7027
by: Paul Morrison | last post by:
Hi all, I need to come up with some differences between arrays in Java and C, I have searched Google and so far all I have found is the following: Arrays in Java are reference types with automatic allocation of memory. In C, arrays are groups of variables of the same type in adjacent memory. Allocation for dynamic arrays is handled by the programmer. This is an 8 mark question in an old exam paper, so I am assuming there are
4
4845
by: Matt F | last post by:
Hey all, I'm a hobbyist programmer who usually codes in C++ or Java. I don't claim to be experts at either, and I'm not familiar with different types of variables, inheritance systems, etc. I've decided to play around with C# and .NET 2.0 a little bit, but I'm still not completely certain about the differences between C++ and C#. All I know is that there are some limitations that aren't on C++ (which is probably a good thing... I think...
0
11198
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
10791
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...
0
10442
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9609
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7993
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
7150
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5829
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
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4647
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.