When is a mem-initializer required?

When is a mem-initializer required?

A const member or a reference member cannot be initialized within an object’s constructor. Instead, they must be initialized in the constructor’s member-initializer list. Conceptually, mem-initialization takes place before the constructor, so by the time it is invoked, all const and reference members have already been assigned a value:

 class mem {	int &ri;  const int k; //both must be mem-initialized 	mem(int i, int j) : ri(i), k(j) {} };

Similarly, when a constructor in a derived class has to pass arguments to its base class constructor, a mem-initializer must be used:

 class base {	int num1;	char * text;	//no default constructor, arguments must be supplied	base(int n1, char * t) {num1 = n1; text = t; }};class derived : public base {	char *buf	derived (int n, char * t) : base(n, t) { buf = new char[100];}};
Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular