Like Tree2Likes
  • 2 Post By Elysia

Question about functors syntax

This is a discussion on Question about functors syntax within the C++ Programming forums, part of the General Programming Boards category; Hi, All ! I have a doubt regarding some syntax used in functors. For example, we have this: [code] class ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    4

    Question Question about functors syntax

    Hi, All !
    I have a doubt regarding some syntax used in functors. For example, we have this:

    [code]
    class myFunctorClass
    {
    public:
    myFunctorClass (int x) : _x( x ) {}

    int operator() (int y) { return _x + y; }
    private:
    int _x;

    }; [c/ode]

    I know that the essential thing that makes one functor works is overloading the operator(), as shown in the code copied here above.
    But my doubt is regarding the constructor syntax:

    Code:
     myFunctorClass (int x) : _x( x ) {}
    The only syntax where I have seen that colon ( : ) in a constructor, is the syntax we use in a derived class to inherit from a base class whose constructor needs a parameter.
    So, my question:
    ¿Which is the detailed meaning of this syntax?

    Code:
     myFunctorClass (int x) : _x( x ) {}
    Thanks in advance!
    Last edited by acerrano; 06-28-2013 at 12:42 PM. Reason: Correcting mistake

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    It's called an initialisation list.
    10.1 — Constructor initialization lists « Learn C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    If you are using C++11, you can use lambdas instead of functors. They are more convenient.
    King Mir and Elkvis like this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    Hi !
    Thanks a lot for your answers!
    Elysia: I needed to know about that constructor syntax, even though another function (lambda, for example) may be better to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple operators in functors
    By a.mlw.walker in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2013, 05:58 PM
  2. List of Functors
    By golfinguy4 in forum C++ Programming
    Replies: 13
    Last Post: 09-29-2009, 01:53 PM
  3. for_each and functors
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 08-10-2009, 07:00 AM
  4. Storing functors?
    By Elysia in forum C++ Programming
    Replies: 18
    Last Post: 11-13-2008, 02:21 AM
  5. Functors
    By cboard_member in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2006, 09:47 AM

Tags for this Thread


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