C++ Reading char variable as a number - How can this be improved?
I wanted to read a char variable from a stream not as a character but as number in a 'elegant' way. After a long search i've come with a solution that can be used like this:

    unsigned char x;
    
    // x will be read as a number
    // ie you type 5 and x will store 5
    cin >> asnumber(x);
    
    // x will be read as a character
    // ie you type 5 and x will store the ascii value of '5'
    cin >> x;

I think this is easy to use and 'elegant' but im no sure if the implementation can lead to some errors of if it can be improved in some way. Here it is, this goes in the .h file:

    class asnumber
    {
    	public:
    		asnumber(char& var);
    		asnumber(unsigned char& var);
    	protected:
    
    	private:
    		char* sdest;
    		unsigned char* udest;
    
    	friend istream& operator>> (istream& in, asnumber instance);
    };
    
    istream& operator>> (istream& in, asnumber instance);

This goes in the .cpp file:

    asnumber::asnumber(char& var){
    	sdest = &var;
    	udest = NULL;
    }
    
    asnumber::asnumber(unsigned char& var){
    	sdest = NULL;
    	udest = &var;
    }
    
    istream& operator>> (istream& in, asnumber instance) {
    	short sx;
    	unsigned short ux;
    
    	if (instance.sdest != NULL) {
    		in >> sx;
    		*(instance.sdest) = sx;
    	}
    	else {
    		in >> ux;
    		*(instance.udest) = ux;
    	}
    
    	return in;
    }

ALSO, IF THIS IS USEFUL TO YOU, USE IT!!!

Thanks

PD: If i have bad english, excuse me.
C++ Reading char variable as a number - How can this be improved?
===

I wanted to read a char variable from a stream not as a character but as number in a 'elegant' way. After a long search i've come with a solution that can be used like this:

    unsigned char x;
    
    // x will be read as a number
    // ie you type 5 and x will store 5
    cin >> asnumber(x);
    
    // x will be read as a character
    // ie you type 5 and x will store the ascii value of '5'
    cin >> x;

I think this is easy to use and 'elegant' but im no sure if the implementation can lead to some errors of if it can be improved in some way. Here it is, this goes in the .h file:

    class asnumber
    {
    	public:
    		asnumber(char& var);
    		asnumber(unsigned char& var);
    	protected:
    
    	private:
    		char* sdest;
    		unsigned char* udest;
    
    	friend istream& operator>> (istream& in, asnumber instance);
    };
    
    istream& operator>> (istream& in, asnumber instance);

This goes in the .cpp file:

    asnumber::asnumber(char& var){
    	sdest = &var;
    	udest = NULL;
    }
    
    asnumber::asnumber(unsigned char& var){
    	sdest = NULL;
    	udest = &var;
    }
    
    istream& operator>> (istream& in, asnumber instance) {
    	short sx;
    	unsigned short ux;
    
    	if (instance.sdest != NULL) {
    		in >> sx;
    		*(instance.sdest) = sx;
    	}
    	else {
    		in >> ux;
    		*(instance.udest) = ux;
    	}
    
    	return in;
    }

ALSO, IF THIS IS USEFUL TO YOU, USE IT!!!

Thanks

PD: If i have bad english, excuse me.