Struct Day {
explicit Day(int d)
: val(d) {}
int val;
};

what does the colon in the 3rd line means?

Thanks

Recommended Answers

All 4 Replies

The second line is a constructor. Constructors are allowed to have initialization lists. This simply means that since val is a member of Day, it val be set to d by the initialization call. Initialization lists follow this fomat:

Struct Generic{
    int a;
    float b;
    char c;
   Generic( int x, float y, char z ) : a( x ), b( y ), c( z ){
        //any other setup that needs to be done
    }
};

So, the colon indicates that an intialization list follows.

This :

class Test{
int i;
public : 
 Test() : i(0){}
};

is very similar to this :

class Test{
int i;
public:
Test(){ i = 0; }
};

The only difference is that the first one is more efficient, and as pointed
out, its called an initializer list.

Struct Day {
explicit Day(int d)
: val(d) {}
int val;
};
what does the colon in the 3rd line means?

I compiled this on my cygwin system and I got this error:
test.c:5: error: expected specifier-qualifier-list before ‘explicit’
Here is what I tried

#include <stdio.h>

struct day
{
	explicit day(int d)
		: val(d){}
	int val;
};
main()
{
	printf("%d\n", sizeof(struct day));
	return 0;
}

You're in C++ now, friend. It won't work if you compile it as C (I'm not up on language extensions and C99 at all but I still don't believe so).

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.