Afficher un message
Vieux 30/06/2008, 17h56   #2
Jerry Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: what is this syntax about?

In article <0bbf9974-42d1-482a-b568-
af1a7e23c10c@m44g2000hsc.googlegroups.com>, campyhapper@yahoo.com
says...
> I've seen this in C++, not Objective C:
>
> namespace bar {
> struct foo {
> foo (int a, int b) :
> int a_,
> int b_
> {}
> };
> }
>
> My question is, what are the colon and post-colon expression there
> for?


As it stands right now, it's syntactically incorrect. My guess is that
what you saw was more like this:

struct foo {
int a_;
int b_;

foo(int a, int b) : a_(a), b_(b) {}
};

This is an initializer list -- i.e. it tells the ctor how to initialize
a_ and b_. In the case above, it's essentially the same as:

foo() { a_ = a; b_ = b; }

The difference is that in this case, we're assigning to a_ and b_
instead of initializing them. In some cases (e.g. references) you can
only initialize, NOT assign, so you _must_ use an initializer list
instead of an assignment.

--
Later,
Jerry.

The universe is a figment of its own imagination.
  Réponse avec citation
 
Page generated in 0,05249 seconds with 9 queries