I just started an advanced C++ course and my instructor sent around a file with this piece of code:

class smallobj		// specify a class
		smallobj() : iData(0){} // Constructor
		smallobj(int iInValue) : iData(iInValue) {} // Overloaded Constructor
		~smallobj() {}		// Destructor
		void setdata(int d)	// member function (sets iData)
			{ iData = d; }	// define the function here because 
					// it's so short
		void showdata()		// member function to display iData
			{cout << "\nThe Value of data is: " << iData; }
		int iData;

I'm confused by the prototypes of the constructors. I've seen some of the other members here write similar prototypes/definitions in their posts. I've also seen VC++ add this information to the constructors in its auto-generated class definitions.

I've done some searches for " : in C++ function definition" and similar but didn't find anything that seemed relevant (actually got several threads in the PHP forum as well).

I've also looked through a couple tutorial documents and my textbook, no luck. The closest I have been able to find is in relation to class inheritance. I think this may be something like that, but that doesn't make sense to me. It can't be part of the ternary operator, there isn't any '?'. The MS-VC++ help has a worthless blurb that calls it some sort of segment operator which just confused me more. It seems more that it's some sort of auto-declaration/initialization shortcut.

What exactly does this type of notation do?


Edited by Fbody: n/a

7 Years
Discussion Span
Last Post by Fbody
Featured Replies

Ive never used them before because I have not learned yet, but you Colon is an inheritance operator. Not much help, sorry =(

unclear, if you don't know, then don't post

Initialization list(s), interesting.

So, if I'm understanding correctly, the concept is that you are using the contained objects' named (as opposed to default) constructor method(s) to set the initial value(s) rather than relying on their assignment statements to modify them after the fact?


Now that I know what I'm looking for, I found it in my textbook.
Thanks again.

Edited by Fbody: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.