PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Syntax I dont understand...

Hi all,

I've come across a syntax I don't understand a few times, and maybe some of you can help. Here is an example:

Code:

CLASS MyClass
{
public:
TCHAR *g_pString;

// constructor
MyClass(void) : g_pString(NULL)
{ }
};


The "g_pString(NULL)" I don't understand. I know that is sets the variable to NULL, but I don't understand why it's allowed after the constructor and the ":" that separates them confuses me even more.

Can anyone explain this to me? I haven't been able to find good information about this yet. Are there any advantages to using this instead of initializing the variable in the body of the function?

Thanks!

--Eric
Is that an exact copy of the code? If so, why is CLASS capitalized? Is it defined to work different from lowercase class?

I don't know what purpose that code serves, but the uppercase CLASS seems suspicious.
Pejo Software - Per · 17 Aug 2003, 19:16 UTC
This can be useful in many cases for example if you have a member variable in your class that doesn't have a default constructor:
Code:
class A
{
public:
A(int i);
};

class B
{
public:
B():m_A(20) {}

A m_A;
};


Initalizing m_A is not possible in the body.

It can also be used to select which constructor to call in a base class and define the order which your initializations shall be done.
Jasper · 17 Aug 2003, 21:02 UTC — Syntax I dont understand...
efortier wrote:
Can anyone explain this to me? I haven't been able to find good information about this yet. Are there any advantages to using this instead of initializing the variable in the body of the function?


The section between the ':' and before the '{' is the member initialisation list for the constructor of that class.

Uses & Advantages:

1) Const and reference class members can only be initialized, so they must be in this list.

2) Initialising here in the initialisation list can also improve effiency. Normally if other types(classes) are "assigned" in the body of the constructor itself, the default constructor for that type is called and then it's assignment operator to actually assign the initial value. Do it within the initialisation list, then only the copy constructor is called to create that type with the required initial value.

BTW All this stuff is covered in great depth in Item 12 & 13 of "Effective C++", by Scott Meyers. I would highly recommend getting this book and it's sequel :)

Jasper
efortier · 17 Aug 2003, 23:18 UTC — Syntax I dont understand...
Jasper wrote:BTW All this stuff is covered in great depth in Item 12 & 13 of "Effective C++", by Scott Meyers. I would highly recommend getting this book and it's sequel :)

I think someone has both those books around here somewhere, I'll go ask!

Thanks a lot for the answers.

--Eric
mlepage · 17 Aug 2003, 23:22 UTC — Initialization vs. Assignment
The above answers highlight the difference between initialization and assignment, which is not always obvious to the amateur programmer.

C++ has a syntax for assignment, like this:

int n;
n = 3;

But that syntax is also, in some cases, an initialization:

int n = 3;

This is also initialization:

int n(3);

I think. I believe it may also be a function declaration. Consult the Meyers book mentioned above!

So, the ':' syntax is a method of initializing in the constructor (good style, BTW). Anything in the body of the constructor must be an assignment.

As noted, some members (e.g. references and const) require initialization, not assignment. And although there is a syntax for array intialization, I don't believe it can be used in a member initialization list for a constructor.