Replace simple function with object of same signature

S

shaun roe

I am about to replace a plain function which returns a string with an
object; I am trying to preserve the interface to clients of the
function, while imbuing the function with some internal state.

lets say the function is

string foo(int myInt);

I thought to make an object with constructor f(int myInt) and hide the
default constructor, copy, assignment (anything else?), but provide some
overloaded string conversion operators, with prototypes

foo::eek:perator string ();
foo::eek:perator char * ();

Questions:
1) Does this work?
2) Do I need to provide const versions of the overloaded conversions?

3) Is there a better way, possibly using just operator()?

eventually the client has to use it the same way as a function, like

string myString=f(5);

or
const string myString=f(4);


???


many thanks

shaun
 
V

Victor Bazarov

shaun said:
I am about to replace a plain function which returns a string with an
object; I am trying to preserve the interface to clients of the
function, while imbuing the function with some internal state.

lets say the function is

string foo(int myInt);

I thought to make an object with constructor f(int myInt) and hide the
default constructor, copy, assignment (anything else?), but provide
some overloaded string conversion operators, with prototypes

foo::eek:perator string ();
foo::eek:perator char * ();

I wonder what your 'char*' returns? A local array? A 'new'ed pointer?
Questions:
1) Does this work?

Why don't you simply try it, and you will see.
2) Do I need to provide const versions of the overloaded conversions?

That's up to you. You're designing your program. Do you ever use
a const 'foo' object?
3) Is there a better way, possibly using just operator()?

I'd say.
eventually the client has to use it the same way as a function, like

string myString=f(5);

or
const string myString=f(4);


???

What's 'f'? How does the client get his hands on 'f'?

V
 
S

shaun roe

"Victor Bazarov said:
I wonder what your 'char*' returns? A local array? A 'new'ed pointer?

Well, I can see where this is going wrong now (thanks); the pointer is
returned but points to a temporary. Presumably I need a 'newed' pointer?
but then I have to worry about a delete later?

Aha... when I look at using a function object with overloaded operator
(), it seems like I have to instantiate the object first. i.e. instead
of the user writing

string myString = foo(myParam);

s/he has to do

foo userFoo;
string myString = userFoo(myParam);

is there something I missed about operator() overloading which would
avoid this? (I suspect so)

Thanks for taking the time to reply!
s.
 
J

John Carson

shaun roe said:
Aha... when I look at using a function object with overloaded operator
(), it seems like I have to instantiate the object first. i.e. instead
of the user writing

string myString = foo(myParam);

s/he has to do

foo userFoo;
string myString = userFoo(myParam);

is there something I missed about operator() overloading which would
avoid this? (I suspect so)

If you want to keep the user typing the same function name, then obviously
you would use a different name for the class --- say Foo --- and declare

Foo foo;

so the user can enter

string myString = foo(myParam);

As for the

Foo foo;

declaration, you can do that yourself rather than relying on the user to do
it. More precisely, you declare

extern Foo foo;

in the header file and then add

Foo foo;

in a .cpp or library file.

That is what is done with cout and cin.
 
D

David Harmon

On Sat, 21 Jan 2006 14:49:33 +0100 in comp.lang.c++, shaun roe
I am about to replace a plain function which returns a string with an
object; I am trying to preserve the interface to clients of the
function, while imbuing the function with some internal state.

I don't think you can do that. Before there was no object in the
interface, now there is, so how can it not change?
lets say the function is

string foo(int myInt);

Let's say the client has a function
void bar(string(*)(myInt));

Before he could pass a pointer to foo, as an argument to bar.
If you change foo, and now there is an object involved, he can no
longer do that without changes. You have changed the interface.
3) Is there a better way, possibly using just operator()?

What are you trying to accomplish?

If you construct the object for every call, then what can it contain
that is of any more use than ordinary local variables in the
original function?

If you construct one object named foo and call operator() on it
repeatedly, what about that is any more use than ordinary static
variables in the function?
 
D

Daniel T.

shaun roe said:
I am about to replace a plain function which returns a string with an
object; I am trying to preserve the interface to clients of the
function, while imbuing the function with some internal state.

lets say the function is

string foo(int myInt);

I thought to make an object with constructor f(int myInt) and hide the
default constructor, copy, assignment (anything else?), but provide some
overloaded string conversion operators, with prototypes

foo::eek:perator string ();
foo::eek:perator char * ();

Questions:
1) Does this work?
2) Do I need to provide const versions of the overloaded conversions?

3) Is there a better way, possibly using just operator()?

eventually the client has to use it the same way as a function, like

string myString=f(5);

or
const string myString=f(4);

Try something like this:

class Foo {
std::string operator()(int) {
// implement
}
} foo;

Why do you want to do this?
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top