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.

Dani AI

Generated

Brief clarification for (and a nod to and ): the single colon after a constructor name starts the member‑initializer list. That list tells the compiler which constructors to call for base classes and member objects before the constructor body runs. The colon is also used in class inheritance declarations, so was partly right, but its role inside a ctor is different.

Key points and practical notes:

  • The initializer list constructs base subobjects and members. The constructor body runs afterward.
  • For class-type members, using the initializer list calls their constructors once. Assigning inside the body may default-construct first and then assign — extra work and sometimes impossible.
  • Const members, reference members, and members of types with no default constructor must be initialized in the initializer list. Base classes likewise must be initialized there if nondefault construction is needed.
  • Initialization order is fixed: base classes first, then members in the order they are declared in the class, not the order written in the initializer list. Mismatched order produces warnings and can cause bugs.

Best practice: prefer initializer lists for class-type members and for any const/reference members. Keep initializer expressions simple and match the members' declaration order to avoid surprises. For modern C++ there are additional tools (in-class initializers, brace init, delegating ctors), but the initializer list remains the correct place to construct bases and members.

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.