logical operators on objects?

This is a discussion on logical operators on objects? within the C++ Programming forums, part of the General Programming Boards category; Hi, i saw this somewhere: Code: mysqlpp::Row row; mysqlpp::Result result = query.store(); if (result && (result.num_rows() > 0) && (row ...

  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 01:49 PM. Reason: changed "logical end" to "AND operator"

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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, 04:22 AM
  3. Renderable Versus Logical Objects
    By Shamino in forum Game Programming
    Replies: 5
    Last Post: 01-21-2006, 02:12 PM
  4. Relational and Logical Operators
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 03-03-2002, 03:33 PM
  5. logical operators
    By sballew in forum C Programming
    Replies: 4
    Last Post: 09-04-2001, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21