Thread: passing operator as a parameter

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question passing operator as a parameter

    Hi,

    I want to have a function that evaluates 2 char vars (like a truth table)

    for example:

    since

    T | F : T

    then

    Code:
    myfunc('t', 'f', ||);  /*defined as: bool myfunc(char lv, char rv, ????)*/
    should return true;

    how can I pass the third parameter? (I know is possible to pass it as a char* but then I will have to have another table to compare operator string and then do the operation which is something I would like to avoid)

    Is it possible to pass an operator like ^(XOR) or ||(OR) or &&(AND), etc in a function/method?

    Thanks in advance
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You could pass in a function (like operator||). If you do, though, keep in mind that operator||('f', 'f') is true.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tabstop View Post
    You could pass in a function (like operator||). If you do, though, keep in mind that operator||('f', 'f') is true.
    Also keep in mind that operators cannot be overloaded for basic types - to overload an operator, a user defined type - usually a class - needs to be involved.

    It is not, for example, possible to overload operator|| for two char's.

    To answer the original question, yes it is necessary to implement some form of mapping between data passed (eg a string "or") and the operation performed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    15
    Why do you want to compare characters? Probably it would be better to create a function that converts chars to booleans, and then it is easy to use any standard operators.

    Code:
    bool CharToBool(char c)
    {
        if (c=='t' || c=='T') return true;
        return false;
    }
    
    int main()
    {
        bool r;
        r=CharToBool('t')||CharToBool('f');
        r=CharToBool('T')&&CharToBool('F');
    }
    Last edited by modwind; 12-26-2010 at 06:13 AM.

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    It sounds like you want to use function objects or function pointers. (On second thought, you never want to use function pointers in C++)

    Here's an example using templates.

    Code:
    template <class T> bool myfunc(bool lv, bool rv, T oper)
    {
    	return oper(lv, rv);
    }
    
    struct Or
    {
    	bool operator()(bool lv, bool rv)
    	{
    		return lv || rv;
    	}
    };
    
    int main()
    {
    	bool result = myfunc(true, false, Or());
    	return 0;
    }
    Here's the same thing using inheritance

    Code:
    struct LogicalOperator
    {
    	virtual bool operator()(bool lv, bool rv) const = 0;
    };
    
    struct Or: public LogicalOperator
    {
    	bool operator()(bool lv, bool rv) const
    	{
    		return lv || rv;
    	}
    };
    
    bool myfunc(bool lv, bool rv, const LogicalOperator& oper)
    {
    	return oper(lv, rv);
    }
    
    int main()
    {
    	bool result = myfunc(true, false, Or());
    	return 0;
    }
    I have the feeling you won't understand all of this, so don't be afraid to ask questions.
    Last edited by NeonBlack; 12-28-2010 at 05:36 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 03:23 PM
  2. parameter passing and pointers
    By pokiepo in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 12:13 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM