Passing anytype parameter to function?

Dec 16, 2008 at 1:57pm
I need a nice function which displays a message and then waits for input, setting the inputted value to a reference parameter passed or/and return it within the function. I have no idea on how to achieve this with whatever datatype i want (the output can be a string, char, int, double, etc.).

This is what i've come up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int message(string mess, int& result)
{
    int output;
    //--stuff--
    cout<<mess<<endl;
    cin>>output;
    if (result != 0) result = output;
    return output;
}

int main()
{
    int value;
    message("Hello", value);
    cout<<value;
    system("PAUSE");
}


This of course works with integer, but how can I make it accept as an argument any type of variable? I was thinking about pointers, but they're tied to a datatype too.

Any help appreciated.
Last edited on Dec 16, 2008 at 2:44pm
Dec 16, 2008 at 2:31pm
You can't. C++ is strictly typed.

This is where generic programming comes in (AKA templates).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <limits>
using namespace std;

//----------------------------------------------------------------------------
// This is your message() function, fixed in certain ways.
// Remember, cin>>foo means that 'foo' is input. You've gotten
// yourself into naming troubles because you have two variables
// that represent the same thing, and the input is also the function's
// return value. I would re-think what exactly it is you are trying to do...
//
// Also, you cannot hard-code a value like zero in a comparison if the
// object may not be a numeric type. I'm not sure why you did this,
// especially as your original program never initialized main()::value,
// so whether or not the function returns a result in value is undefined.
//
template <typename ValueType>
ValueType message(
  const string&    mess,
  ValueType&       result,
  const ValueType& result_must_differ_from_this_to_be_assigned
  ) {
  ValueType input;

  cout << mess << endl;
  cin >> input;

  if (result != result_must_differ_from_this_to_be_assigned)
    result = input;

  return input;
  }

//----------------------------------------------------------------------------
// Here's another datatype (other than integer) to play with.
// It is just a string that >> reads terminating at newline instead of
// any-old whitespace.
//
typedef string linestring;

inline istream& operator >> ( istream& ins, linestring& s )
  {
  return getline( ins, s );
  }

//----------------------------------------------------------------------------
// And here's the main function, much like your example.
// Notice how value1 will not be modified, but value2 will (since 42 != ~42).
//
int main()
  {
  int value1 = 0;
  int value2 = 42;

  message( "Enter an integer", value1,   0 );
  message( "Enter another",    value2, ~42 );

  cout << "value1 = " << value1 << '\n'
       << "value2 = " << value2 << '\n';

  // Get rid of that extra newline waiting in the input after reading value2
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  // Play with strings instead of system()
  linestring foo;
  message( "Press Enter to quit...", foo, linestring( "\1" ) );
  if (!foo.empty())
    {
    cout << "Ah, but \"" << foo << "\" is more than just 'Enter'. :-)\n";
    }

  return 0;
  }

The thing to remember is that this code generates two message() functions: one that parameterizes on int and one that parameterizes on linestring.

Hope this helps.

[edit] Oh, yeah, try playing with the input to the program... Have fun!
Last edited on Dec 16, 2008 at 2:32pm
Dec 16, 2008 at 2:50pm
Wow! That is amazing! :D
Thank you a lot for the help!

Btw that (result != 0) was a line I forgot to delete, cause I had tried to insert it as a default parameter in the function (int& result=0), therefore not needing to pass the variable (which I suppose would be making the = operator a function to pass it?).
Dec 16, 2008 at 7:23pm
By the way... what is the difference between passing the string as a const string reference and const string non-reference? (apart from the fact that the ref. is the address of the constant)
Dec 16, 2008 at 7:58pm
The former passes a reference to the object that remains in the caller function. The latter makes a bitwise copy, local to the called function, which takes longer.
Topic archived. No new replies allowed.