Newbie: Class usage

V

Victor Bazarov

Looza said:
I have defined a class which takes a string, does some manipulation on it
and returns a character. It's a simple kind of check procedure to ensure a
code has been entered correctly. It is defined as so:

class cLotCheck
{
private:
// some private stuff

char GetCheckChar(const std::string& s);

public:
cLotCheck();
char operator()(const std::string& s) {
return GetCheckChar(s);
}
};

Is it valid C++ to use it as so:

cLotCheck check;
char ch = check("stringvalue");
Absolutely!

The code obviously works, but I was unsure if it is safe to use like this,

Safe? [What's that all talk about safety today?] How could it be unsafe?
If it compiles and does what you need, why do you still have a problem?
or if there is a preferred way (originally it was written as a function, but
I wanted to put it into a class for self-teaching purposes)

You wrote a functor. Congratulations. Now you know that it's possible to
write a class that kind of works like a function. It's a functor. There
are very good uses for functors in C++. You will learn more about those
as you study the standard library.

Victor
 
R

Rob Williscroft

Looza wrote in in
comp.lang.c++:
Hi,

I have defined a class which takes a string, does some manipulation on
it and returns a character. It's a simple kind of check procedure to
ensure a code has been entered correctly. It is defined as so:

class cLotCheck
{
private:
// some private stuff

char GetCheckChar(const std::string& s);

public:
cLotCheck();
char operator()(const std::string& s) {
return GetCheckChar(s);
}
};

Is it valid C++ to use it as so:

cLotCheck check;
char ch = check("stringvalue");

The code obviously works, but I was unsure if it is safe to use like
this, or if there is a preferred way (originally it was written as a
function, but I wanted to put it into a class for self-teaching
purposes)

What you've writen is often refered to as a "functor", the standard
library has lots of them, though they all templated (std::less< T >
for example).

Functor's are often prefered for generic algorithms as compared
to passing a function-pointer, as the algorithm can often call the
actual code inline (which may (or may not) speed things up a bit).

Rob.
 
L

Looza

Hi,

I have defined a class which takes a string, does some manipulation on it
and returns a character. It's a simple kind of check procedure to ensure a
code has been entered correctly. It is defined as so:

class cLotCheck
{
private:
// some private stuff

char GetCheckChar(const std::string& s);

public:
cLotCheck();
char operator()(const std::string& s) {
return GetCheckChar(s);
}
};

Is it valid C++ to use it as so:

cLotCheck check;
char ch = check("stringvalue");

The code obviously works, but I was unsure if it is safe to use like this,
or if there is a preferred way (originally it was written as a function, but
I wanted to put it into a class for self-teaching purposes)

Thanks for reading.
 
D

Daniel T.

Rob Williscroft said:
Looza wrote in in
comp.lang.c++:


What you've writen is often refered to as a "functor", the standard
library has lots of them, though they all templated (std::less< T >
for example).

Functor's are often prefered for generic algorithms as compared
to passing a function-pointer, as the algorithm can often call the
actual code inline (which may (or may not) speed things up a bit).

I don't think that has much to do with the value of functors. What makes
them better is their ability to maintain state and be composed. A simple
example is something like:

binder2nd<greater<int> > foo = bind2nd( greater<int>(), 0 );

Now we have a function that returns true if its argument is greater than
0.

assert( foo( 2 ) );
 
R

Rob Williscroft

Daniel T. wrote in @news01.east.earthlink.net in comp.lang.c++:
I don't think that has much to do with the value of functors.

Value and prefered are orthogonal concepts.
What makes
them better is their ability to maintain state and be composed. A
simple example is something like:

binder2nd<greater<int> > foo = bind2nd( greater<int>(), 0 );


Now we have a function that returns true if its argument is greater than
0.

assert( foo( 2 ) );

However, use something more sophisticated than std::bind2nd and
all isn't lost, and the *composability* isn't limited to functor's:

#include <cassert>

template < typename T >
inline bool func( T const &lhs, T const &rhs )
{
return lhs > rhs;
}

template < typename T >
struct Functor
{
bool operator () ( T const &lhs, T const &rhs ) const
{
return lhs > rhs;
}
};


#include "boost/bind.hpp"
#include "boost/function.hpp"

int main()
{
using namespace boost;

function< bool(int const &) > foo =
bind< int >( Functor<int>(), _1, 0 );

assert( foo( 2 ) );

function< bool(int const &) > foo2 = bind( func<int>, _1, 0 );
assert( foo2( 2 ) );
}

Note that its the functor that need's extra information to be supplied
to do the composition, the <int> after bind or it needs a result_type
member typedef.

Adding state is also possible (and arguably less error prone), both
of the examples above do it, i.e. the bound argument 0.

Rob.
 
J

jeffc

Victor Bazarov said:
Safe? [What's that all talk about safety today?] How could it be unsafe?
If it compiles and does what you need, why do you still have a problem?

Oh boy.
 
D

David Hilsee

jeffc said:
Victor Bazarov said:
Safe? [What's that all talk about safety today?] How could it be unsafe?
If it compiles and does what you need, why do you still have a problem?

Oh boy.

Heh. I think that might be the most common statement uttered before a very
long debugging session. ;-)

Just joshing, of course. I think Victor was just trying to say that Looza's
concerns about safety were completely unjustified.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,485
Latest member
Andrewayne909

Latest Threads

Top