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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-18-2006, 12:15 PM
|
#1
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
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?
|
|
|
|
06-18-2006, 12:50 PM
|
#2
|
|
Member
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189
Rep:
|
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;
}
|
|
|
|
06-18-2006, 01:08 PM
|
#3
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Original Poster
Rep: 
|
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.
|
|
|
|
06-18-2006, 01:41 PM
|
#4
|
|
Member
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189
Rep:
|
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.
|
|
|
|
06-19-2006, 05:42 PM
|
#5
|
|
Senior Member
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524
Rep:
|
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.
|
|
|
|
06-19-2006, 10:46 PM
|
#6
|
|
Member
Registered: Aug 2005
Location: Albuquerque, NM
Distribution: Fedora
Posts: 30
Rep:
|
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?
|
|
|
|
06-20-2006, 04:19 PM
|
#7
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Original Poster
Rep: 
|
OK, got it. Thanks.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:33 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|