Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   What does this syntax mean? (http://www.velocityreviews.com/forums/t621689-what-does-this-syntax-mean.html)

Giuseppe:G: 06-22-2008 07:02 PM

What does this syntax mean?
 
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

Ian Collins 06-22-2008 07:15 PM

Re: What does this syntax mean?
 
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.

David Côme 06-22-2008 07:22 PM

Re: What does this syntax mean?
 
On Sun, 22 Jun 2008 21:02:45 +0200, G: <giuseppegalloneNO@spamgmail.com>
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.

Tim Slattery 06-23-2008 02:20 PM

Re: What does this syntax mean?
 
Giuseppe:G: <giuseppegalloneNO@SPAMgmail.com> 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
Slattery_T@bls.gov
http://members.cox.net/slatteryt


All times are GMT. The time now is 11:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.