This topic is 4704 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

consider the following code:
class A {
public:
A(int i) : i(i) {
}

private:
int i;
};

int main()
{
A a;

return 0;
}


The code cannot compile because there is no default constructor of class A, but the book says that "the compiler will generate the default constructor if you don't define it" What's wrong ?

##### Share on other sites
What compiler are you using? Alternatively, it's pretty easy to do:

class A {public:        A() {} //HERE'S THE DEFAULT CONSTRUCTOR	A(int i) : i(i) {	}		private:	int i;};int main(){	A a; 	return 0;}

##### Share on other sites
That behavior is correct. A Default constructor is generated if no constructor is supplied; but you supplied a constructor, supressing the generation of the default.

##### Share on other sites
I'm using VC2005 Beta2 and gcc 3.3.3
Neither of them compile

##### Share on other sites
Again; the behavior is correct. It should not generate a default constructor.

However, it might also fail because of this: i(i). If that works, it's still extremely poor style.

##### Share on other sites
You supplied a constructor, so no default constructor is generated making this:

	A a;

un-compile-able

(is that even a word?)

Seems like that would work in Java, however.

##### Share on other sites
If you define your constructor with a default argument in the parameter list, you can use the constuctor both ways.

##### Share on other sites
Quote:
 "the compiler will generate the default constructor if you don't define it"

This should read "the compiler will generate the default constructor if you don't define any constructor". Otherwise you will need to specify one. Generally this is not a real problem - as you're surely aware, initializer lists are your friends.

Quote:
 Original post by DeyjaHowever, it might also fail because of this: i(i). If that works, it's still extremely poor style.

It does work (because the scoping rules are different inside and outside the brackets), and there's nothing wrong with it; I've seen it endorsed implicitly by many prominent users around here (including myself).

##### Share on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by DeyjaHowever, it might also fail because of this: i(i). If that works, it's still extremely poor style.

It does work (because the scoping rules are different inside and outside the brackets), and there's nothing wrong with it; I've seen it endorsed implicitly by many prominent users around here (including myself).

Agreed, and as a fact I use this method all the time.

The variables named in an initializer list as being initialized must come from the class itself, so the outer i is allways the class member, in this case the integer listed right after the private: label. You can't do:
int unrelated;struct foo {    foo( int pie )        : unrelated( pie )    {    }}

Similarly, the i supplied in the constructor's argument list will shadow the member variable as per normal scoping rules. This means that the inner i is allways the argument. It's good style, it's defined behavior by the standard, and it's sexy good. Or something.

##### Share on other sites
Quote:
 Original post by Anonymous PosterIf you define your constructor with a default argument in the parameter list, you can use the constuctor both ways.

he/she means

struct foo {  int i;  foo(int j = 0)  : i(j) {}}

[Edited by - snk_kid on July 5, 2005 2:24:44 PM]

• 23
• 10
• 19
• 16
• 14