Thread: what is : operator ?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    what is : operator ?

    what is : operator ? what it does ? i know about scope resolution operator. it is written as :: but what is : operator ?
    can anybody explain plz. i dont know the name of this operator.

    if anybody give very simple example, will be glad.

    probabily these are written inside a class.

    thanks
    blue_gene

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    There is no such thing as operator :

    However, there is the ternary operator, which uses ? and :

    Code:
    #include <iostream>
    
    int main()
    {
        bool expression = true;
        char result = expression ? 't' : 'f';
        std::cout << result << std::endl;
    
        expression = false;
        result = expression ? 't' : 'f';
        std::cout << result << std::endl;
    
        int var = 10;
        result = var == 10 ? 't' : 'f';
        std::cout << result << std::endl;
    
        var = 20;
        result = var == 10 ? 't' : 'f';
        std::cout << result << std::endl;
    }
    If the expression evaluates to true, then the first value is returned, otherwise the second value is returned. I think you can overload the ternary operator for a class, but I think it is rarely a good idea.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    ? : is the tertiary operator.


    a < b ? a : b;

    the above line is the equivalent of

    if(a < b)
    return a;
    else
    return b;

    In more general terms it says if the conditional statement before the ? operator is true then return the variable before the : operator, else return the variable after the : operator.

    : is also used as in initialization lists for classes, and maybe more. ? may be used for other purposes as well. But when used together, that's what it does.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I think you can overload the ternary operator for a class
    No, the ternary conditional operator is one of the operators that cannot be overloaded.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    i am well aware of the ternary operator in C . i have seen these to use for the class constructor.i am sorry, i dont have the example right now . but is there any other way to use this operator in a class ? or any other application except this ?
    blue_gene

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i have seen these to use for the class constructor.
    You mean something like this?
    Code:
    myClass::myClass ( int a, int b, int c )
      : mya ( a )
      , myb ( b )
      , myc ( c )
    {}
    The colon simply says that what follows will be an initialization list followed by the constructor body instead of just the constructor body. The ? operator is only used in conjunction with : as the conditional operator, but : is used elsewhere (bit fields, inheritance, labels).
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    exactly i was talking about that.

    The colon simply says that what follows will be an initialization list followed by the constructor
    not clear.

    what is the equivalent code of that ?
    your brackets are not properly written probabily
    blue_gene

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what is the equivalent code of that ?
    In this case:
    Code:
    myClass::myClass ( int a, int b, int c )
    {
      mya = a;
      myb = b;
      myc = c;
    }
    >your brackets are not properly written probabily
    ...
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    yes,latter one i know and that is very simple . i also read about initialization list from my book.

    i wonder, how many ways a simple thing can be made complicated !! there are many syntaxes for the same thing!!

    anyway, its fine....ok

    thanks
    blue_gene

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how many ways a simple thing can be made complicated !!
    You really don't want to know.

    >there are many syntaxes for the same thing!!
    Well, when it comes to initialization lists, there is a purpose. Create a class with a const or reference member and see how far you get trying to initialize it in the constructor body.
    My best code is written with the delete key.

  11. #11
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    You all forgot about when using switches

    Code:
    switch(variable) {
         case something:
              statement;
              break;
    etc.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You all forgot about when using switches
    >>but : is used elsewhere (bit fields, inheritance, labels)
    My best code is written with the delete key.

  13. #13
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    You caught me prelude, I forgot about labels and bitfields, haven't done too much with inheritence yet, I still am learning c++. I guess the : operator is the misc. operator. Do all know all operator.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 01:23 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM