|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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.) |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|