C++ syntax. what is colon followed by...

I come from a C# background. I'm trying to interpret the following C++ code:
/* A typical Library with a collection of books.
 * This library will publish an event to all members whenever a new book is arrived
class Library
   vector<Book> bookList; //Collection of books.
   Library() :
    newBookEvent(this) //Set the source of event as the current object
   string libraryName;
   GoFPatterns::Event<Library, Book&> newBookEvent;  //Declaring an event
						//which will be fired when a
				          	//new book is added to library
    * Library public constructor which initializes library name and the Event
   Library(string name) :
    libraryName(name), newBookEvent(this) //Set the source of event will
            //be the current object

    * Function to add a new book to library
   void AddBook(Book newBook)
    //Add the book to collection
    //Fire the newBookEvent with the newly added book as argument

Open in new window

We are defining a class named Library

It has a private array of Books (the vector) named bookList

Then there's a private constructor? named Library() which has no arguments. After that is a colon : , and what is newBookEvent(this)? I'm not sure what this colon notation means.

There's a public string libraryName

a public variable newBookEvent which holds an instance of class Event from namespace GoFPatterns

A public constructor Library with one string parameter (name), again a colon and libraryName(name) comma, newBookEvent(this). That's where I get lost.

Finally a void function AddBook which takes one parameter of type Book class, which has 2 lines of executable code.
deleydSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The ': starts the costructor initializer list, see http://www.cprogramming.com/tutorial/initialization-lists-c++.html  it is used to call base class constructors or initialize members before the constructor body is executed. A simple example would be

class foo {

  foo(int n) : val(n) {}

  foo() : val(0) {}


int val;


Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.