CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    PLease explain this syntax

    could some please explain this syntax for C++ functor
    Code:
    struct add_x {
      add_x(int x) : x(x) {}   // please  expain this
      int operator()(int y) { return x + y; } // and this
    I was interested is the colon : after add_x(int x)

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: PLease explain this syntax

    That's an initializer list. You may find one in any constructor.

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: PLease explain this syntax

    The second one is the operator for casting to int, both explicitly and implicitly with static_cast<int>(object)
    Last edited by ninja9578; September 5th, 2011 at 09:11 PM.

  4. #4
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: PLease explain this syntax

    I know for experienced developers it it might be mundane stuff , please elaborate I am try to port an application to CUDA thrust C++ API , the real easy way is to pass a functor , similar to
    std:: transform with vector array CUDA in parallel will perform all calculations in paralles , the thrust::transform works similar to std version except it is a whole lot faster ,

    For some reason I have difficulty warping my head around C++ functor construct ( may be a mental block )

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: PLease explain this syntax

    Quote Originally Posted by ninja9578
    The second one is the operator for casting to int, both explicitly and implicitly with static_cast<int>(object)
    No, the second one overloads the function call operator such that this function can be invoked using syntax that makes it appear like an add_x object is a function that takes an int argument and returns an int, i.e., a function object (functor).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Re: PLease explain this syntax

    Quote Originally Posted by ninja9578 View Post
    The second one is the operator for casting to int, both explicitly and implicitly with static_cast<int>(object)
    Correct me if I'm wrong, but I think the second one is an overload of the function call operator that returns the sum of the argument and the data member as an int. So, you could do something like this:
    Code:
    add_x obj(50);
    const int y = obj(10);
    The value of y would be 60.
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: PLease explain this syntax

    Quote Originally Posted by aamir121a View Post
    could some please explain this syntax for C++ functor
    You could do this instead,

    Code:
    struct add_x {
      add_x(int a) {x = a;}   // variable assignment of constructor parameter not using an intialization list
      int fun(int y) {return x + y};  // definition of your own functor function not following the () convention
    For constructors it's considered best practice to use an initialization list. It can also be a little faster.

    You can define any functor function you like but since most functor enabled code follows the overloaded () operator convention it's better to stick with it.
    Last edited by nuzzle; September 6th, 2011 at 07:10 AM.

  8. #8
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: PLease explain this syntax

    thank you all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured