Thread: logical operators on objects?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    19

    logical operators on objects?

    Hi,
    i saw this somewhere:
    Code:
    			mysqlpp::Row row;
    			mysqlpp::Result result = query.store();
    
    
    if
    (result && (result.num_rows() > 0) && (row = result.at(0)))  {
    ...
    }
    "Row" and "Result" are objects... What is the meaning of applying the AND operator on objects?

    Thanks
    Last edited by nocturna_gr; 12-19-2007 at 02:49 PM. Reason: changed "logical end" to "AND operator"

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, I think && is more commonly called "logical AND" and & "bitwise AND".

    The meaning depends on the implementation, e.g., in this case making a mysqlpp::Result convertible to bool probably means that the query succeeded if it evaluates to true.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It follows the same pattern as "stream" objects, where you can check like this:
    Code:
    fstream fin;
    fin.open("somefile.txt");
    if (fin) ...
    But it depends on the implementation of the class itself - if there's no implementatin to make a bool (or something the compiler can easily translate to a bool, like an int) from the object, then it is not leglal to perform this sort of operation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Do the mysqlpp::Result and mysqlpp::Row classes have an operator bool() defined?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do the mysqlpp::Result and mysqlpp::Row classes have an operator bool() defined?
    According to some online mysqlpp docs I looked up, they do, inherited from a parent class. I wonder why they did not try an operator void* or even the safe bool idiom.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Perhaps they predate (widespread knowledge of) that idiom, or the author was simply not familiar with it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Codeblocks and logical operators
    By fhbwghads in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2008, 05:22 AM
  3. Renderable Versus Logical Objects
    By Shamino in forum Game Programming
    Replies: 5
    Last Post: 01-21-2006, 03:12 PM
  4. Relational and Logical Operators
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 03-03-2002, 04:33 PM
  5. logical operators
    By sballew in forum C Programming
    Replies: 4
    Last Post: 09-04-2001, 06:24 PM