How to get and pass data to a class member function

P

pcnate

My class in a tech school, has kinda been debating over how you would
get and pass data to a fuction in C++.

What we would like to do is to pass information to a function directly
from a cin kinda like this

cin >> className.memFunc( ??? )
^^^>---------------------->^^^


is this possible in a single line of code?
 
G

guyarad

I haven't tried to implement what you are asking,
But I think you can operator overload the >> operator, so the first
argument will be istream and the second will pointer to function (of
the type of your function).
Than you can do something like this:
cin >> className.memFunc
 
R

Rolf Magnus

My class in a tech school, has kinda been debating over how you would
get and pass data to a fuction in C++.

What we would like to do is to pass information to a function directly
from a cin kinda like this

cin >> className.memFunc( ??? )
^^^>---------------------->^^^


is this possible in a single line of code?

Let className.memFunc() return a reference to the object you want to read
into. This will violate encapsulation, but it will do what you want.
 
V

Victor Bazarov

My class in a tech school, has kinda been debating over how you would
get and pass data to a fuction in C++.

What we would like to do is to pass information to a function directly
from a cin kinda like this

cin >> className.memFunc( ??? )
^^^>---------------------->^^^


is this possible in a single line of code?

No, usually not. What does your 'memFunc' expect? If it's a single
char, you could do

objectName.memFunc(cin.get());

but if you have something else, like a double, then no, you'd have to
wrap getting into another function, which usually constitutes more than
one line of code.

Actually, if you pass 'cin' directly into that function and let the
function to read all it needs, then you could write

objectName.memFunc(cin);

of course...

V
 
V

Victor Bazarov

Rolf said:
Let className.memFunc() return a reference to the object you want to
read into. This will violate encapsulation, but it will do what you
want.

What if that function has side effects that depend on the value passed?
Then the application of the function should be delayed until the reading
is complete. It can be accomplished with a proxy object, but it most
likely be more than one line of code :)

V
 
G

guyarad

Here. something like that:

#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

BOOL Func(string& str)
{
MessageBox(NULL, str.c_str(), "Surprise", MB_OK);
return TRUE;
}

typedef BOOL (*tpFunc)(string&);

istream &operator >> (istream& in, tpFunc pFunc)
{
string str;
in >> str;
(*pFunc)(str);
return in;
}

int main()
{
cin >> Func;
return 0;
}

Of course the types here quite strict, but you can template this...
 
T

Tomás

What we would like to do is to pass information to a function directly
from a cin kinda like this


Opps, I misunderstood the question first time around. Maybe something like
the following:



#include <iostream>
using std::cin; using std::cout; using std::endl; using std::istream;

class CrazyFunctionHelper {

static void ActualFunc(int const i)
{
cout <<
"I'm the actual function, and my argument's value is "

<< i << "\n\n";

}

public:


friend istream& operator>>(istream&, const CrazyFunctionHelper&);

};


istream& operator>>(istream &is, const CrazyFunctionHelper &cfh)
{
int i;

is >> i;

cfh.ActualFunc(i);
}


int main()
{
cin >> CrazyFunctionHelper();
}



-Tomás
 
P

Puppet_Sock

My class in a tech school, has kinda been debating over how you would
get and pass data to a fuction in C++.

What we would like to do is to pass information to a function directly
from a cin kinda like this

cin >> className.memFunc( ??? )
^^^>---------------------->^^^


is this possible in a single line of code?

Oh, it's probably possible. But it's probably not desirable.

As others have said, you can do stuff like having memFunc return
a reference to something.

But try it the other way. You want a class to be busy reading data.
Why don't you have the class read the file? Really delegate. Don't
just use a class as a big-old-plastic-bag to hold stuff in. Move the
functionality down into the class. Move all the file stuff down into
the class and hide it all away from prying eyes and busy coder
hands that want to add bugs to it. Don't even let the clients of
the class know there is a file to be opened and read.
Socks
 
P

pcnate

there is no file! I was just trying to find a simple way of accepting
input and passing it to a class function without actually putting it in
the class, cause that is not the purpose of the class. It looks as if
it is just simpler to accept input on one line and pass it on the next.
It just seemed so natural to do the above type of thing but it looks
more advanced than our first class program! Thanks anyway.
 

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

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top