LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-27-2006, 05:08 PM   #1
bx.s
LQ Newbie
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 13

Rep: Reputation: 0
question about C++ syntax


I am coming from a Java background, and only have briefly played with C++. I am trying to understand what is going on in this mozilla function, (some mozilla seamonkey code) but I do not understand the syntax and cannot find anything like it in a book.
Code:
 nsHttpConnection::nsHttpConnection()
      : mTransaction(nsnull)
      , mConnInfo(nsnull)
      , mLock(nsnull)
 <snip (more syntax like this)>
 {
//function contents
<snip>
 }
So I am assuming this is a constructor method. The syntax that I find confusing is the colon and list of private instance variables after the function name. My guess is that this is another way of setting initial values to these, but I want to check with somebody who knows before I proceed with this assumption.

Thank you in advance for your insight.
 
Old 07-27-2006, 05:19 PM   #2
excel28
Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 72

Rep: Reputation: 15
Yep, thats how I understand it.
 
Old 07-27-2006, 05:22 PM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,401

Rep: Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120Reputation: 1120
Yep. They're default values.

"Seamonkey code." I like that.
 
Old 07-27-2006, 07:50 PM   #4
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
Quote:
Originally Posted by bx.s
So I am assuming this is a constructor method. The syntax that I find confusing is the colon and list of private instance variables after the function name. My guess is that this is another way of setting initial values to these, but I want to check with somebody who knows before I proceed with this assumption.
Yes, this is how to initialize member variables of an instance. It calls the constructor for each of the members with the arguments that you give it.

If you do not do this, then members will be initialized with the default constructor (no arguments); which may not be desirable.

For class types, if you do not provide an initializer and assign something to it later, that is bad because it has to construct it using the default constructor, and then assign something else using the assignment operator.

Also, some types (references and const types) cannot be assigned to or copied to, and therefore must be initialized this way.

In Java, you did not have to deal with this because all values are either primitive types or reference types, and so don't need to be initialized specially.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ syntax question jiml8 Programming 6 06-20-2006 04:19 PM
rnc Syntax Question Linux31 Linux - Networking 1 10-07-2005 05:28 AM
Syntax question satimis Linux - Newbie 9 09-23-2004 07:47 AM
Wget - syntax question satimis Linux - Software 3 09-11-2004 03:29 AM
Syntax question satimis Linux - Newbie 4 10-08-2003 09:33 PM


All times are GMT -5. The time now is 01:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration