4 Replies - 1437 Views - Last Post: 23 April 2009 - 04:07 PM Rate Topic: -----

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Int, float, and double setting

Posted 23 April 2009 - 09:37 AM

Hi everybody,
Does anyone know of a way to write a function that can set any kind of number? I've learned that void pointers are in general a bad idea, but it seems like something like this would be useful. I have a test program where I've tried to do this, like so:

#include <iostream>
#include <cstdlib>
using namespace std;

int a = 22;
float b = 44;
double c = 66;

void assignValue( double* val, char* line ) {
  *(val) = atof( line );
}

int main( int argc, char* argv[] ) {
  cout << a << " " << b << " " << c << endl;
  assignValue( &a, "33" );
  cout << a << " " << b << " " << c << endl;
  assignValue( &b, "66" );
  cout << a << " " << b << " " << c << endl;
  assignValue( &c, "99" );
  cout << a << " " << b << " " << c << endl;
  return 0;
}



And I get these errors when I try to compile:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:15: error: cannot convert ‘int*’ to ‘double*’ for argument ‘1’ to ‘void assignValue(double*, char*)’
test.cpp:17: error: cannot convert ‘float*’ to ‘double*’ for argument ‘1’ to ‘void assignValue(double*, char*)’
test.cpp:19: warning: deprecated conversion from string constant to ‘char*’

I'm not worried about the warning on line 19, my real program won't have a string literal in it so that warning doesn't exist.

From what it seems like in the other two warnings, though, you can't convert a double* to int* or float*. I could, of course, create a separate function for each number type (int, float, double), but I thought this might be a little more efficient if I could do it all at once.

So, does anyone know of a way to do this, or am I barking up the wrong tree and I should just separate the types into different functions?

Thanks,
Zel2008

Is This A Good Question/Topic? 0
  • +

Replies To: Int, float, and double setting

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Int, float, and double setting

Posted 23 April 2009 - 09:46 AM

What do you mean by "set any kind of number".

Expand on what exactly you want to achieve.
Was This Post Helpful? 0
  • +
  • -

#3 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Int, float, and double setting

Posted 23 April 2009 - 10:00 AM

After a while, I realized that my next rambling post was probably even more confusing than the last one. What I want to do, put simply, is learn how to combine these three functions:

void assignValue( double& val, char* line ) {
  val = atof( line );
}

void assignValue( float& val, char* line ) {
  val = atof( line );
}

void assignValue( int& val, char* line ) {
  val = atof( line );
}

// All these functions also include error checking to make sure the new value > 0



Into one function, so I can get rid of all the repetitive code. I don't think I can use a template, because this is a function in a class that has to be compiled into an object.

Thanks,
Zel2008

This post has been edited by Zel2008: 23 April 2009 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#4 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Int, float, and double setting

Posted 23 April 2009 - 02:46 PM

Hi everybody,
I did some more research on templates and realized I simply hadn't understood what they're capable of; if anyone else wants to know, you can define a template function, along with its code, in a header file, and then use that template for whatever data type you wish, as long as the data type can legitimately be used in all calls within that template function. The function can be either public or private and it will still work in the implementation file. I haven't tested this with inheritance; that's for another day, but in using a simple header and implementation file, that's how to do it.

Hope that helps somebody,
Zel2008

This post has been edited by Zel2008: 23 April 2009 - 02:47 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Int, float, and double setting

Posted 23 April 2009 - 04:07 PM

Moved to C++ then? Much easier.

Here's an implementation of what you're trying to do:
#include <iostream>
#include <sstream>

using namespace std;

template<class T>
void assignValue( T& val, const string &line ) {
	static stringstream ss (stringstream::in | stringstream::out);
	ss << line;
	ss >> val;
}

int main( int argc, char* argv[] ) {
	int a = 22;
	float b = 44;
	double c = 66;
	cout << a << " " << b << " " << c << endl;
	assignValue( a, "33" );
	cout << a << " " << b << " " << c << endl;
	assignValue( b, "66" );
	cout << a << " " << b << " " << c << endl;
	assignValue( c, "99" );
	cout << a << " " << b << " " << c << endl;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1