473,659 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Jun 30 '08 #1
5 1260
In article <0bbf9974-42d1-482a-b568-
af**********@m4 4g2000hsc.googl egroups.com>, ca*********@yah oo.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.
Jun 30 '08 #2
On Jun 30, 11:56*am, Jerry Coffin <jcof...@taeus. comwrote:
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.
Jun 30 '08 #3
On Jun 30, 12:16 pm, campyhap...@yah oo.com wrote:
On Jun 30, 11:56 am, Jerry Coffin <jcof...@taeus. comwrote:
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.arachs ys.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.
Jun 30 '08 #4
Daniel T. wrote:
>Now I see why Java only has objects, no references.

The above is wrong.

<http://www.yoda.arachs ys.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.)
Jul 1 '08 #5
On Jun 30, 11:05*am, "Daniel T." <danie...@earth link.netwrote:
>
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
Jul 1 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
19000
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
54
6544
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature here, that feature there. Variations on style are of no interest to me. I'm coming at this from a...
28
3287
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are a number of aspects to this simplification, but for me the unification of methods and functions is the biggest benefit. All methods look like functions (which students already understand). Prototypes (classes) look like modules. This will...
3
2990
by: Chris Cioffi | last post by:
I started writing this list because I wanted to have definite points to base a comparison on and as the starting point of writing something myself. After looking around, I think it would be a waste of time to start writing yet another IDE and so am now thinking in terms of new features/plug-ins for existing systems. Right now I mostly use Komodo Personal and it's pretty close to being what I want. They don't currently support plug-ins,...
121
10025
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
8
4033
by: Hermawih | last post by:
Hello , I want your opinion about this . In order to say it clearly , I think I have to describe it in long sentences . I could consider myself as Intermediate/Advance Access Developer ; Intermediate/Advanced Database designer . Because of the requirements , I must create Web Application . Access Pages is not suitable for that so I think about learning VB Net / ASP Net . I am
17
1503
by: Kai Glaesner | last post by:
Hello community, I read this group for several days now and wonder what kind of questions one can expect to find answered here, or what the charter of this group is. The name implies "CSharp" (the language) but looking at the postings it seems more like "anything I encounter when using CSharp and the .Net framework". And if it's that general, what good are the other groups
19
1802
by: G.Ashok | last post by:
Hi All, What is your weightage of the 3 characteristics of Object-Oriented Programming i.e. Inheritance, Encapsulation and Polymorphism. for a total of 100% Please quote your values and let's discuss how the people thinking about OOP :-) Regards,
167
8277
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an object should run in its own thread, it should implement this mix-in class. Does this sound like plausible design decision? I'm surprised that C++ doesn't have such functionality, say in its STL. This absence of a thread/object relationship in...
14
9844
by: gimme_this_gimme_that | last post by:
What is going on here with the dollar signs and parenthesis? $(document).ready(function(){ $("li").behavior("click",function(){ $(this).load(menu.html"); }); }); And when do you use click vs. onclick? Is onclick only an attribute thing?
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.