LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 06-18-2006, 12:15 PM   #1
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
C++ syntax question


I have run across a construction with which I am not familiar, and I do not understand it.

Basically, I have a constructor function that looks like this:

Myclass::Myclass(type1 var1, type2 var2, ... double T) : P(T)
{
implementation of constructor
}

Now, I do not understand the : P(T) that follows the constructor definition. In this case, the double T definition is referring to Temperature in degrees Kelvin. It is likely though not certain that the subsequent T in the P(T) is also temperature, but I don't know. It can't be a base class definition because this isn't a class definition statement.

P is used in the code for the constructor, therefore I presume that this is an array of temperatures that is somehow being passed in for initialization, but I don't recognize the syntax.

Can someone here tell me what is going on?
 
Old 06-18-2006, 12:50 PM   #2
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
This is THE way how you should most often initialize your members within a Construcor. It is semantically equal to:
Code:
Myclass::Myclass(type1 var1, type2 var2, ... double T)
{
  P = T;
  //implementation of constructor
}
...but the difference is, that in the first (good) solution you save the default constructor call of P and initialize it directly, so you get a better performance.

This is an equivalent logic like in the following (more common) example:
Code:
void foo_not_so_good(){
    Complex c;
    c = (Complex)5;
}
 
void foo_much_better(){
    Complex c = 5;
}
 
Old 06-18-2006, 01:08 PM   #3
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 116Reputation: 116
My question is that I do not understand the syntax of this line:

Code:
Myclass::Myclass(type1 var1, type2 var2, ... double T) : P(T)
I thought that P(T) might be an array, but it can't be (would have to be P[T]).

So, what is this? Is P(T) an alternate constructor that only takes T as an argument?

In this case, would something like this:
Code:
void Foo(type1 arg1, ... , typeN argN) : X(argN) { some code}
give me effectively an overloaded function, that I could refer to as X(var) if I only wanted to enter argN ?

Basically, the question is: what does the single colon accomplish in this situation?

Last edited by jiml8; 06-18-2006 at 01:10 PM.
 
Old 06-18-2006, 01:41 PM   #4
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
This single colon indicates the start of the member initialization list. After this colon you can initialize your member variables of the class (or struct). In your case the member 'P' is constructed with a constructor that takes the 'double T'.
Another example:
Code:
class Foo{
  public:
    Foo(std::string s1, std::string s2, int i) 
      : m_s1(s1), m_s2(s2), m_i(i) {}
  private:
    std::string m_s1;
    std::string m_s2;
    int m_i;
};
As I said in my first post, an alternative to this would be:
Code:
class Foo{
  public:
    Foo(std::string s1, std::string s2, int i) {
      m_s1 = s1;
      m_s2 = s2;
      m_i = i;
    }
  private:
    std::string m_s1;
    std::string m_s2;
    int m_i;
};
The reason why you should use the first method is because you save the default constructor calls of both (m_s1 and m_s2) and initialize them directly (as I said already). In the case of 'm_i' it is actually just a matter of personal preference and won't give you much advantages.

So these things after the colon are no function-headers or additional constructors or such strange things, but only variable initializations.

Hope things are clearer now?

Last edited by Flesym; 06-18-2006 at 01:43 PM.
 
Old 06-19-2006, 05:42 PM   #5
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
It's not only for performance (and I suspect a decent compiler will optimize out redundant assignments if it can): you can't modify const members in the constructor body, but you can construct them with the ": P(T)" syntax.
 
Old 06-19-2006, 10:46 PM   #6
mhcox
Member
 
Registered: Aug 2005
Location: Albuquerque, NM
Distribution: Fedora
Posts: 30

Rep: Reputation: 15
Quote:
Originally Posted by jiml8
My question is that I do not understand the syntax of this line:

Code:
Myclass::Myclass(type1 var1, type2 var2, ... double T) : P(T)
I thought that P(T) might be an array, but it can't be (would have to be P[T]).

So, what is this? Is P(T) an alternate constructor that only takes T as an argument?

In this case, would something like this:
Code:
void Foo(type1 arg1, ... , typeN argN) : X(argN) { some code}
give me effectively an overloaded function, that I could refer to as X(var) if I only wanted to enter argN ?

Basically, the question is: what does the single colon accomplish in this situation?
You sure Foo doesn't have a base class X that takes an arugment of typeN?
 
Old 06-20-2006, 04:19 PM   #7
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 116Reputation: 116
OK, got it. Thanks.
 
  


Reply



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
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
iptables syntax question Poetics Linux - Security 4 12-24-2003 03:32 PM
Syntax question satimis Linux - Newbie 4 10-08-2003 09:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:11 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration