LinuxQuestions.org

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

jiml8 06-18-2006 12:15 PM

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?

Flesym 06-18-2006 12:50 PM

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;
}


jiml8 06-18-2006 01:08 PM

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?

Flesym 06-18-2006 01:41 PM

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?

jonaskoelker 06-19-2006 05:42 PM

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.

mhcox 06-19-2006 10:46 PM

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?

jiml8 06-20-2006 04:19 PM

OK, got it. Thanks.


All times are GMT -5. The time now is 08:58 PM.