question about default constructor

Started by
14 comments, last by xEricx 18 years, 9 months ago
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 ?
Advertisement
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;}
The best way to predict the future is to invent it.
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.
I'm using VC2005 Beta2 and gcc 3.3.3
Neither of them compile
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.
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.

my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
If you define your constructor with a default argument in the parameter list, you can use the constuctor both ways.
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 Deyja
However, 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).
Quote:Original post by Zahlman
Quote:Original post by Deyja
However, 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.
Quote:Original post by Anonymous Poster
If 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]

This topic is closed to new replies.

Advertisement