What does this syntax mean?

Discussion in 'C++' started by Giuseppe:G:, Jun 22, 2008.

  1. Giuseppe:G:

    Giuseppe:G: Guest

    Hi, the following piece of code is rather obscure for me

    struct Foo
    {
    Foo( int _x ) : x(_x) {}
    ....
    };

    what's the meaning of that construct? I understand Foo() must be a
    constructor, right? But then what's with the syntax after the ":" ??

    Thanks!
    Giuseppe
     
    Giuseppe:G:, Jun 22, 2008
    #1
    1. Advertising

  2. Giuseppe:G:

    Ian Collins Guest

    Giuseppe:G: wrote:
    > Hi, the following piece of code is rather obscure for me
    >
    > struct Foo
    > {
    > Foo( int _x ) : x(_x) {}
    > ....
    > };
    >
    > what's the meaning of that construct? I understand Foo() must be a
    > constructor, right? But then what's with the syntax after the ":" ??
    >

    Look up initialiser lists.

    --
    Ian Collins.
     
    Ian Collins, Jun 22, 2008
    #2
    1. Advertising

  3. On Sun, 22 Jun 2008 21:02:45 +0200, G: <>
    wrote:

    > Hi, the following piece of code is rather obscure for me
    >
    > struct Foo
    > {
    > Foo( int _x ) : x(_x) {}
    > ...
    > };
    >
    > what's the meaning of that construct? I understand Foo() must be a
    > constructor, right? But then what's with the syntax after the ":" ??
    >
    > Thanks!
    > Giuseppe


    Search "initialization lists" on google.
     
    David Côme, Jun 22, 2008
    #3
  4. Giuseppe:G:

    Tim Slattery Guest

    Giuseppe:G: <> wrote:

    >Hi, the following piece of code is rather obscure for me
    >
    >struct Foo
    >{
    > Foo( int _x ) : x(_x) {}
    >...
    >};


    Foo takes an integer argument. the :x(_x) says to initialize the
    member variable x using the argument _x. "{}" says that the body of
    the method is empty, that is, the initialization is the only thing
    that the constructor does.

    --
    Tim Slattery

    http://members.cox.net/slatteryt
     
    Tim Slattery, Jun 23, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Feverish
    Replies:
    4
    Views:
    1,034
    Stewart Gordon
    Jan 24, 2005
  2. Casey
    Replies:
    9
    Views:
    258
    Robert Kern
    Oct 4, 2007
  3. Li Ma
    Replies:
    1
    Views:
    2,282
    Roedy Green
    Mar 9, 2009
  4. Rahul
    Replies:
    4
    Views:
    578
    Robert Kern
    Apr 7, 2009
  5. C Barrington-Leigh
    Replies:
    1
    Views:
    1,209
    Tim Leslie
    Sep 10, 2010
Loading...

Share This Page