Otterprinz
Otterprinz

Reputation: 469

C++ Constructor code...what is this called?

This is what I found in a program's code:

pff::NAS::NAS( const NAS& p_Other ) 
: pff::MCCI(_T("NAS"))           //<- ?
, m_strS(_T("JustAString"))      //<- ?
, m_strK(_T("JustAString"))      //<- ?
, m_strR(p_Other.GetmystrR())    //<- ?
, m_Swap()
{ }

And my Question is: What are those (//<- ?)-marked lines called? I'd love to search for what its supposed to do and why the person who did this code used it.

Upvotes: 2

Views: 128

Answers (4)

hamon
hamon

Reputation: 1016

Those are for initializing your fields in the class. This is how it is done:

class MyClass{
private:
    int my_int;
public:
    MyClass(int a):my_int(a){}

the my_int field contains now the value of a. What you do is calling the constructor of the field you want to initialize.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 60017

Those lines are initializing the variables for that object

i.e.

pff::MCCI  (inherited)
m_strS 
m_str

Upvotes: 2

Lyubomir Vasilev
Lyubomir Vasilev

Reputation: 3030

This is called Initializer list. It is used to initialize the values of the class/struct member variables. You can see more about this in this nice article.

Upvotes: 5

Tristram Gr&#228;bener
Tristram Gr&#228;bener

Reputation: 9711

It's called initialization list.

More information in the excellent FAQ http://www.parashift.com/c++-faq/init-lists.html

Upvotes: 8

Related Questions