| | |
Class Constructor Shorthand
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I've been reading through a beginning chapter on classes and I have come upon this below:
class Critter
{
public:
Critter(const string& name = ""): m_Name(name) {}
...
};
I was wondering why they wrote the constructor this way. If anyone can point me to some info, I would be very happy.
class Critter
{
public:
Critter(const string& name = ""): m_Name(name) {}
...
};
I was wondering why they wrote the constructor this way. If anyone can point me to some info, I would be very happy.
If in doubt, reach into the trash can and remove the user guide.
Constructors can initialise through assignment operator or initialisation lists. Benefit of using initialisation lists is the improved performance. Your code will run faster if you use initialization lists rather than assignment.
http://www.parashift.com/c++-faq-lit....html#faq-10.6
http://www.parashift.com/c++-faq-lit....html#faq-10.6
•
•
•
•
Originally Posted by sunnypalsingh
Constructors can initialise through assignment operator or initialisation lists. Benefit of using initialisation lists is the improved performance. Your code will run faster if you use initialization lists rather than assignment.
http://www.parashift.com/c++-faq-lit....html#faq-10.6
Is it shorthand? If so, is there a long way of writing it?
If in doubt, reach into the trash can and remove the user guide.
I believe you are reading too far into my question. I appreciate your help. Let me define my question more specifically.
One person defined a class constructor this way:
Another person defined a class constructor this way:
Besides the fact the two constructors are from different classes. What is the difference of writing it the first way apposed of writing it the second way?
I noticed that the first initializes the members after a colon and the second initializes the members inside the curley braces. So what is the difference?
One person defined a class constructor this way:
C++ Syntax (Toggle Plain Text)
Critter::Critter(int hunger, int boredom): m_Hunger(hunger), m_Boredom(boredom) {}
Another person defined a class constructor this way:
C++ Syntax (Toggle Plain Text)
Box::Box(int ht, int wd, int dp) { height = ht; width = wd; depth = dp; }
Besides the fact the two constructors are from different classes. What is the difference of writing it the first way apposed of writing it the second way?
I noticed that the first initializes the members after a colon and the second initializes the members inside the curley braces. So what is the difference?
If in doubt, reach into the trash can and remove the user guide.
Oh so my first post was using an initializing list. I was under the impression that it was the same as an assignment constructor, just written in "shorthand" I thought. So when you use an assignment constructor it passes a temporary object that isn't efficient. Yet an initializing list does it all in one instance. Cool
One last question about the initializing list.
When you create your Fred object, will anything that is passed to whatever be assigned to x_ using the initializing list?
One last question about the initializing list.
C++ Syntax (Toggle Plain Text)
Fred::Fred() : x_(whatever) {}
When you create your Fred object, will anything that is passed to whatever be assigned to x_ using the initializing list?
If in doubt, reach into the trash can and remove the user guide.
Another small thing to add that I read in the book C++ in Action .
Say you have a const member declared inside the C++ class.
since the weight member cant be assigned a value in a standard contructor like this
The only way you can do that is using an initialization list.
Say you have a const member declared inside the C++ class.
C++ Syntax (Toggle Plain Text)
class Planet { public: Planet( int iWeight); private: const int weight; }
since the weight member cant be assigned a value in a standard contructor like this
C++ Syntax (Toggle Plain Text)
Planet::Planet( int iWeight) { weight = iWeight ; // wrong }
C++ Syntax (Toggle Plain Text)
Planet::Planet( int iWeight):weight(iWeight) // correct { }
•
•
•
•
Originally Posted by WolfPack
Another small thing to add that I read in the book C++ in Action .
Say you have a const member declared inside the C++ class.
C++ Syntax (Toggle Plain Text)
class Planet { public: Planet( int iWeight); private: const int weight; }
since the weight member cant be assigned a value in a standard contructor like this
The only way you can do that is using an initialization list.C++ Syntax (Toggle Plain Text)
Planet::Planet( int iWeight) { weight = iWeight ; // wrong }
C++ Syntax (Toggle Plain Text)
Planet::Planet( int iWeight):weight(iWeight) // correct { }
So an Initialization list has the ability to indirectly access the data member weight and maintain encapsulation, right?
If in doubt, reach into the trash can and remove the user guide.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Segmentation Fault
- Next Thread: C++ second largest problem
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






