0

I have an example functor deceleration as;

#include <iostream>

class myFunctorClass
{
    public:
        myFunctorClass (int x) : _x( x ) {}
        int operator() (int y) { return _x + y; }
    private:
        int _x;
};

int main()
{
    myFunctorClass addFive( 5 );
    std::cout << addFive( 6 );

    return 0;
}

My "What" is about the line just after the public:. I do not understand the syntax of the line myFunctorClass (int x) : _x( x ) {}. What is doing and what another instance of the use of such syntax rule?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
erogol
  • 13,156
  • 33
  • 101
  • 155
  • 9
    It's a constructor, get [a good book](http://tinyurl.com/so-cxxbooks). – Xeo May 24 '13 at 13:39
  • 1
    And the functor is not slowing down, so "functor decelaration" is not a very meaningful term – Andy Prowl May 24 '13 at 13:40
  • It initializes the private member _x to the value specified when calling the constructor. So _x is initialized to 5 or 6 in your case even before the constructor actually runs. – hetepeperfan May 24 '13 at 13:41
  • possible duplicate of [What is this weird colon-member syntax in the constructor?](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) – hmjd May 24 '13 at 13:46

4 Answers4

1

myFunctorClass (int x) : _x( x ) {} is the constructor. It's a function that is executed whenever an object of the class is instantiated.

The : _x(x) part is an initialization list. That means it initializes a member with a value (in this case the value x is used to initialize _x), or intializes a parent class.

The code would in this case be functionally equal to

myFunctorClass (int x) {
    _x = x; 
}

Note this equivalence is not always true. There are slight differences between initialization and assignment and copy/move-construction.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
Appleshell
  • 7,088
  • 6
  • 47
  • 96
  • is it just for constructor or might be used for other function definitions. – erogol May 24 '13 at 13:53
  • @Erogol the initializer list is only for constructors. It would make no sense anywhere else. – rubenvb May 24 '13 at 14:02
  • Intialization lists can only be used in constructors. As the name implies, it's just there to _initialize_ the members or parents for the first time. This is especially useful for `const` members, as they can not be re-assigned, or parent classes that take constructor parameters themselves. – Appleshell May 24 '13 at 14:03
0

It's simply the definition of myFunctorClass's constructor. Perhaps it is just the : _x( x ) part that is confusing you. Would myFunctorClass (int x) {} make sense to you? The : _x( x ) part is a member initialization list and says that the member _x should be initialised with the value of the argument x.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

That's the class' constructor. It's initializing the data members (after the :). And the function body of the constructor us empty.

Anon Mail
  • 4,660
  • 1
  • 18
  • 21
0

You're using the class constructor member initializer list to initialize your member _x to x that was passed as 5 to the constructor in myFunctorClass addFive( 5 );

Johann Gerell
  • 24,991
  • 10
  • 72
  • 122