LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   question about C++ syntax (http://www.linuxquestions.org/questions/programming-9/question-about-c-syntax-468351/)

bx.s 07-27-2006 05:08 PM

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.

excel28 07-27-2006 05:19 PM

Yep, thats how I understand it.

sundialsvcs 07-27-2006 05:22 PM

Yep. They're default values.

"Seamonkey code." I like that. :D

spooon 07-27-2006 07:50 PM

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.


All times are GMT -5. The time now is 02:42 AM.