Do not understand definition of a special class constructor

Hey guys,

I need your help once again.
I try to understand a class definition .

The code looks like that:
1
2
3
4
5
6
7
8
9
  class matrix {

public:

matrix(int __n=0) : n(__n) {if (n) a=new double[n*n];}
.
.
.
}


I know what the command does:
If I write in main
matrix c(3), I get an array c from type double with 9 spaces.
But I do not understand for what
...(int __n=0) : n(__n) {if (n)...
stands for.

Can someone helps me interpreting it ?
Thanks a lot,
stubborn
What part confuses you? The : n(__n) part? If so, that's an initialization list (a little bit of a misnomer, but what the heck).

See if this helps at all:
http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/

-Albatross
Hey Albatross,

thank's a lot for your answer.
yes, the :n(__n) part is the first thing confusing me.
The link will help for understanding initializing lists.

The other thing is the part of the if-order.
I know requests like if (n=3) or if (n=true) ore other more complicated requirements.
But what means if (n) in that context ?
n is nowhere declared.
First off, you're using the wrong equals in your examples. You probably meant the equality operator (==) instead of assignment. :P

Most basic types (read: basic types like int, char, pointers, and the like) in C++ implicitly cast to bools the following way:
If they're zero, then as booleans they're false.
If they are nonzero, then as booleans they're true.

Basically, that if statement is a shortened way of checking if n is not zero.

-Albatross
Yes, of course I meant nothing other than if ( n==3) :D
Thanks a lot for your help :-)
Topic archived. No new replies allowed.