what is this syntax about?

C

campyhapper

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?
 
J

Jerry Coffin

(e-mail address removed)>, (e-mail address removed)
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.
 
C

campyhapper

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

Daniel T.

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

Juha Nieminen

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

Greg Herlihy

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
 

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,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top