What does this syntax mean?

G

Giuseppe:G:

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
 
I

Ian Collins

Giuseppe:G: said:
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.
 
D

David Côme

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.
 
T

Tim Slattery

Giuseppe:G: said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top