Jump to content
  • Advertisement
Sign in to follow this  
neverland

question about default constructor

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
If you define your constructor with a default argument in the parameter list, you can use the constuctor both ways.

Share this post


Link to post
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 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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!