Class Operator Problem

What i am trying to do is get a class to act as a function when needed
class Foo
{
 char *operator() (char *str) { return "Test"; }
 int v;
}

Foo x; char *TestStr;
TestStr=x("String");

and i also want to still be able to access the variables/ functions inside the class

int myInt = x.v;

i keep getting the error that "term does not evaluate to a function"

any help on this would be great, thanks in advance
KhisanthAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
AxterConnect With a Mentor Commented:
When it's a pointer, You can get it to work using any one of the following syntax:

fst=(*footest)("Test123");
fst=footest->operator()("Test123");
0
 
AxterCommented:
In your example,  both the operator function and member data v, are private.

Try making them public:
0
 
AxterCommented:
class Foo
{
public:
char *operator() (char *str) { return "Test"; }
int v;
};

You were also missing a simicolon at the end of your class statement.
That might also be the problem.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
AxterCommented:
char *operator() (char *str) { return "Test"; }

This operator is returning a temporary string that goes out of scope out side of the function.  This will cause problems.
0
 
AxterCommented:
Try the following:

class Foo
{
public://Add missing public keyword
char *operator() (char *str)
{
 static char Data[] = "Test";
 return Data;
}
int v;
}; //Add missing simicolon
0
 
AxterCommented:
I forgot to mention your opertator should accept a const char*.
Example:

class Foo
{
public://Add missing public keyword
char *operator() (const char *str) //Add const char*
{
static char Data[] = "Test";
return Data;
}
int v;
}; //Add missing simicolon
0
 
AxterCommented:
Last but not least, Add an extra () to the following line:

TestStr=x()("String");
0
 
KhisanthAuthor Commented:
Well i just typed that real fast thats why there are typos and missing the public, but its right in the code

ive tried what you posed and still get this error when trying to do testStr=x()("String");
"term does not evaluate to a function"
0
 
AxterCommented:
>>Well i just typed that real fast thats why there are typos

Instead of typing it in here, could you please copy and paste the code to a comment.

There could be other problems in the code, but we won't know until we see the actual code you're using.
0
 
KhisanthAuthor Commented:
Ok it looks like your way works as long as the class isnt a pointer

class FOO
{
public:
     char *operator()(char *lpl) { return lpl; }
     int x;
};

FOO *footest; char *fst;

footest=new FOO;
fst=footest("Test123");
footest->x=0;
MessageBox(hWnd, fst, "Test", MB_OK);
delete footest;

works if i just do FOO footest; and dont make it a pointer but the way it is it generates the error "error C2064: term does not evaluate to a function"
0
 
AxterCommented:
It should also be able to work like this:
fst=footest->()("Test123");

But I couldn't get that to compile on VC++ 6.0.

The previous two methods I posted will compile.
0
 
KhisanthAuthor Commented:
Worked Perfect, Thanx :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.