Re: differences between a c-function and a class method
From: Jerry Coffin (jcoffin_at_taeus.com)
Date: 02/28/05
- Next message: Howard: "Re: monitoring the heap"
- Previous message: Default User: "Re: best way to modify a file"
- In reply to: gustav04: "differences between a c-function and a class method"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 28 Feb 2005 10:48:14 -0800
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::print2std().
>
> 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+(rational const &);
};
rational operator+(rational 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+(rational(2), x);
or:
y.operator=(operator+(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.
- Next message: Howard: "Re: monitoring the heap"
- Previous message: Default User: "Re: best way to modify a file"
- In reply to: gustav04: "differences between a c-function and a class method"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|