-1

Possible Duplicate:
What is this weird colon-member syntax in the constructor?

Here is a sketch of the code. I do not understand this line - Queue():first(0), last(0){}. What does first(0) mean. I know and understand the idea of queue. I have coded it many times, but don't know what that means in c++.

class Queue{
 struct Element{
    double val;
    Element* next;
 }*first, *last;

 void insert(double x);
 void get(double &x);
public:
    Queue():first(0), last(0){}

};
Community
  • 1
  • 1
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 3
    Okay, I'm having issues trying to close it, but http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor – chris Feb 02 '13 at 00:41
  • 1
    And another: http://stackoverflow.com/questions/2604176/strange-constructor – Jerry Coffin Feb 02 '13 at 00:46

3 Answers3

3

It is an initialization list. It allows you constructing your member variables directly with the values you want to assign them, rather than default-constructing them and then assigning them in the body of your constructor.

Suppose you have a class A with a default constructor and a constructor accepting one argument:

struct A
{
    A() : n(0) { } // For scalar types, n = 0 in the constructor's body is equivalent
    A(int n) : n(a) { } // Should be marked as "explicit", but not relevant here
    int a;
};

And a class X with a member variable of type A. The constructor of X takes an argument of type int which needs to be assigned to A's variable a. Without an initialization list, this is how you would do it:

struct X
{
    X(int n) { a.a = n; } // First invoke A::A(), then assign n to a.a
    A a;
};

But here you have two instructions rather than one: a (potentially expensive) default construction of your member variable a, followed by an assignment to change its state as desired. If you already know the arguments with which a should be constructed, it does not make sense to perform a default construction first just to override that initialization.

It must be said, though, that when initializing scalar types (as is the case of your example), the form based on initialization lists is equivalent to the one based on regular assignments in the constructor's body, since scalar types are not zero-initialized by default (more precisely, they are not initialized in any way).


As a related information, in an initialization list the name of your constructor's arguments won't hide the name of the member variables you want to construct:

struct X
{
    X(int a) : a(a) { } // OK (although I do not like it)
    int a;
};

In a regular assignment, on the contrary, a would be regarded to be the constructor's argument on both sides of the assignment:

struct X
{
    X(int a) { a = a; } // Self-assignment of the constructor's argument...
    int a;
};

This said, I do not consider a good practice to use identical names for member variables and function arguments. I mentioned it because it is good to be aware of this if you will have to read code written by others.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
2

This is a member initialization list. The line Queue():first(0), last(0){} defines a default constructor for Queue that initializes the first and last members to 0 and then does nothing else.

The members first and last are both of type Element* ("pointer to Element") and 0 is a null pointer constant. When initialised with 0, first and last are null pointers.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
2

In this particular case, since we're dealing with scalar types, it's equivalent to:

Queue()
{
    first = 0;
    last = 0;
}
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • but first and last are not numbers but objects – Yoda Feb 02 '13 at 01:20
  • I'm not sure what you're getting at. `first` and `last` are pointers, and you can initialize pointers with the constant zero. Also, pointers *are* scalar types in C++. – fredoverflow Feb 02 '13 at 09:46