// increaser
#include <iostream> //Include iostream
usingnamespace std; //using namespace std
void increase (void* data, int psize) { //function which takes a void pointer and an integer which will hold the size of data's type in bytes.
if ( psize == sizeof(char) ) //if the size matches that of a char integral type...
{ char* pchar; pchar=(char*)data; ++(*pchar); } //create char pointer, point to data, increment data.
elseif (psize == sizeof(int) ) // otherwise, if it matches the size of an integer integral type...
{ int* pint; pint=(int*)data; ++(*pint); } //create int pointer, point to data, increment data.
}
int main () {
char a = 'x';
int b = 1602;
increase (&a,sizeof(a)); //pass a character to increase()
increase (&b,sizeof(b)); //pass an int to increase()
cout << a << ", " << b << endl;
return 0;
I've loosely commented it for you, however, the code is pretty sloppy and bug prone. For instance, what if I pass a boolean? Or any other type for that matter? The byte size could potentially match between a type that's expected and one that isn't.