Thread: Question about functors syntax

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you are using C++11, you can use lambdas instead of functors. They are more convenient.
    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.

  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, 03: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