ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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;
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.