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

class smallobj		// specify a class
{
	public:
		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; }
	private:
		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?

Thanks.

Recommended Answers

All 3 Replies

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

commented: unclear, if you don't know, then don't post +0

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?

Thanks.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.