PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > what is this syntax about?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
what is this syntax about?

Réponse
 
LinkBack Outils de la discussion
Vieux 30/06/2008, 16h40   #1
campyhapper@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut what is this syntax about?

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?
  Réponse avec citation
Vieux 30/06/2008, 16h56   #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
Vieux 30/06/2008, 17h16   #3
campyhapper@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: what is this syntax about?

On Jun 30, 11:56am, Jerry Coffin <jcof...@taeus.com> wrote:

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


Now I see why Java only has objects, no references. The designers of C+
+ sacrificed readability for this albeit clever feature of references.
Not that I'm trolling mind you, the two approaches aren't better or
worse per se, just different.
  Réponse avec citation
Vieux 30/06/2008, 19h05   #4
Daniel T.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: what is this syntax about?

On Jun 30, 12:16 pm, campyhap...@yahoo.com wrote:
> On Jun 30, 11:56 am, Jerry Coffin <jcof...@taeus.com> wrote:
>
> > 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.

>
> Now I see why Java only has objects, no references.


The above is wrong.

<http://www.yoda.arachsys.com/java/passing.html>
"The values of variables are always primitives or references, never
objects."

> The designers of C++ sacrificed readability for this albeit clever
> feature of references.
> Not that I'm trolling mind you, the two approaches aren't better or
> worse per se, just different.


Just to be clear. The difference between the two languages when it
comes to this particular issue isn't about "references", rather it's
about how an object is initialized. In Java, all fields are default
initialized, in C++ the programmer has control over the field's
initialization. For example:

class Foo {
int bar;
public:
Foo(): bar(5) { }
};

In the above C++ code 'bar' is *created* with the value of 5.

class Foo {
private int bar;
public Foo() {
bar = 5;
}
}

In the above Java code, 'bar' is created with the value of 0 and then
assigned the value of 5. Note, near the same thing would happen if we
used equivalent code in C++ except 'bar' would be created with some
undefined value, then assigned the value of 5.

When it comes to ints, the separate creation/assignment steps don't
much matter, but if we were using a type that is expensive to create
and expensive to assign, then being able to save a step is ful.
  Réponse avec citation
Vieux 01/07/2008, 18h41   #5
Juha Nieminen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: what is this syntax about?

Daniel T. wrote:
>> Now I see why Java only has objects, no references.

>
> The above is wrong.
>
> <http://www.yoda.arachsys.com/java/passing.html>
> "The values of variables are always primitives or references, never
> objects."


Also note that Java references and C++ references are not the same
thing. Java references are more like C++ pointers without pointer
arithmetic, and which can only point to dynamically allocated objects.

C++ references are more limited than this. For example, you can't
change an existing reference to point to something else. (A consequence
of this is that there are no null references in C++). A C++ reference is
more like an "alias".

(OTOH C++ references have features Java references don't. For example,
a C++ reference can point to an internal type, as well as a statically
allocated object, while in Java this is not possible.)
  Réponse avec citation
Vieux 01/07/2008, 22h57   #6
Greg Herlihy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: what is this syntax about?

On Jun 30, 11:05am, "Daniel T." <danie...@earthlink.net> wrote:
>
> > The designers of C++ sacrificed readability for this albeit clever
> > feature of references.
> > Not that I'm trolling mind you, the two approaches aren't better or
> > worse per se, just different.

>
> Just to be clear. The difference between the two languages when it
> comes to this particular issue isn't about "references", rather it's
> about how an object is initialized. In Java, all fields are default
> initialized, in C++ the programmer has control over the field's
> initialization.


Then there must be no difference - because Java programmers can also
control a class member's initialization. For example:

class Foo
{
private int bar = 5;
// ...
}

In fact, C++09 will allow C++ programmers to initialize class members
in the same way (and with the same syntax) that Java programmers use
today.

> For example:
>
> class Foo {
> int bar;
> public:
> Foo(): bar(5) { }
>
> };


In C++09, a program could define an identical Foo class like so:

class Foo {
int bar = 5;
public:
Foo() {}
};

> In the above C++ code 'bar' is *created* with the value of 5.


Not really. The constructor initializer-list does indeed initialize
"bar" with the value 5 - but "bar" could very well have already been
initialized with a different value (namely, zero). For example, any
Foo object that has static storage duration will have first been zero-
initialized, before the constructor initializer list executes.
Therefore, bar could not have been "created" with the value 5 -
because 5 is only the second - and not the first - value that was used
to initialize bar.

Greg


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h00.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14106 seconds with 14 queries