CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    [RESOLVED] Variable name

    I have a bunch of functions sharing the variable names like:
    void myfunc(int a, string b);
    void func2(int c, string b);
    void func3(int a, int c);

    But they are not consistent in the way they have been called in functions. But I want to write a general checker for input variables. Something of sort:
    if inputs are like this, then they each should have this value. E.g. 'a' should not be greater than 10, 'b' should not be less than 0, etc. I ask this cause within the function call, 'a', 'b', 'c' don't really exist per se but they contain values instead.

    How do I implement this?

    Thanks guys

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Variable name

    Why not give variable names logical names instead of 1 letter-stuff. 1 letter variables are an abomination for maintaining code.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Variable name

    One way to impose "automatic" checks on variables is to specify a unique type for the variable. For instance, "'a' should not be greater than 10" could be represented by a class IntLessThan10.

    This class would need to be constructable from an int, and optionally, you could even make it implicitly convertible to an int. That way, you could use it almost exactly like an int, but it would have additional logic in the constructor which throws an exception if given an invalid argument.

  4. #4
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    Re: Variable name

    @Lindley

    Can you please elaborate some more on your suggestion, I like your idea but since I am still learning I feel I am missing quite a bit from your suggestion.

    Thanks

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Variable name

    Quote Originally Posted by Learned View Post
    @Lindley

    Can you please elaborate some more on your suggestion, I like your idea but since I am still learning I feel I am missing quite a bit from your suggestion.
    Lindley described in detail as to what to do.

    You need to specify exactly what you didn't understand.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    Re: Variable name

    This is what I have been thinking:
    class lessthan10
    {
    public:
    int a;
    lessthan10() {cout<<"Bad data"; //etc.}
    };

    Now in the original function, if it had been:
    void myfunc(int a, string b);

    How would the user call it:
    myfunc(1000, string b);

    This would not work. Wouldn't the user need to define an object first, etc.

    Thanks

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Variable name

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    class IntLessThan10
    {
    	int m_int;
    public:
    	operator int()
    	{
    		return m_int;
    	}
    
    	IntLessThan10(int int_): m_int(int_)
    	{
    		if (m_int >= 10)
    			throw Exception(m_int);
    	}
    
    	struct Exception
    	{
    		Exception(int int_)
    		{
    			ostringstream str;
    			str << "IntLessThan10: bad value: " << int_;
    			msg = str.str();
    		}
    		string msg;
    	};
    };
    
    void foo(IntLessThan10 a)
    {
    	cout << __FUNCTION__ << ": Succeeded with " << (int)a << endl;
    }
    
    
    int main()
    {
    	try
    	{
    		foo(-12);
    		foo(7);
    		foo(17);
    	}
    	catch(IntLessThan10::Exception e)
    	{
    		cout << e.msg << endl;
    	}
    	
    	return 0;
    }
    Code:
    D:\Temp\23>23
    foo: Succeeded with -12
    foo: Succeeded with 7
    IntLessThan10: bad value: 17
    Best regards,
    Igor

  8. #8
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    Re: Variable name

    Very nicely done. Thanks. I learn a lot here just reading people's code.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: [RESOLVED] Variable name

    Here's a bit more generic solution:
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    struct LessThanException
    {
    	LessThanException(int int_, int threshold)
    	{
    		ostringstream str;
    		str << "IntLessThan" << threshold << ": bad value: " << int_;
    		msg = str.str();
    	}
    	string msg;
    };
    
    template <int t_threshold>
    class IntLessThan
    {
    	int m_int;
    public:
    	operator int()
    	{
    		return m_int;
    	}
    
    	IntLessThan(int int_): m_int(int_)
    	{
    		if (m_int >= t_threshold)
    			throw LessThanException(m_int, t_threshold);
    	}
    };
    
    typedef IntLessThan<10>  IntLessThan10;
    
    void foo(int a)
    {
    	IntLessThan10 dummy(a); // just check
    	cout << __FUNCTION__ << ": Succeeded with " << a << endl;
    }
    
    template <int t_threshold>
    void Foo(int a)
    {
    	IntLessThan<t_threshold> dummy(a); // just check
    	cout << __FUNCTION__ << ": Succeeded with " << a << endl;
    }
    
    
    int main()
    {
    	try
    	{
    		foo(-12);      // threshold prefefined by foo implementation
    		foo(7);
    		Foo<20>(17);   // threshold explicitly specified
    		Foo<15>(17);
    	}
    	catch(LessThanException e)
    	{
    		cout << e.msg << endl;
    	}
    	
    	return 0;
    }
    Code:
    D:\Temp\23>23.exe
    
    foo: Succeeded with -12
    foo: Succeeded with 7
    Foo: Succeeded with 17
    IntLessThan15: bad value: 17
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured