Jump to content
  • Advertisement
Sign in to follow this  
derek7

why need the default constructor?

This topic is 4795 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


class d
{
	int i;
public:
	 
	d(int ii){i = ii;}};

class d1:public d
{
	int i;
public:
	 
	d1(int ii){i = ii;}
};
main()
{	
 
 
 
}


C2512 .why?

Share this post


Link to post
Share on other sites
Advertisement
Man...

Here is my best guess at what that jumble of charcters there should have looked like...

// I renamed d and d1 to A and B for readability
class A
{
int i;
public:
A(int ii) {i = ii;}
};

class B : public A
{
int j; // need a different variable name here! - we already have i from A
public:
// A has no default constructor (C2512) - we must call one manually like this
B(int ii, int jj) : A(ii) {j = jj;}
};


Now for the love of Pete - go and learn C++ properly (RTFM) and stop spamming the forum!

Share this post


Link to post
Share on other sites
Because D is not default constructable.

If B is a sub-type of A, then A's constructor is always invoked before B. When you don't specifiy which constructor of A to use in any of B's constructor initializer lists then A's default constructor is implicitly tryed.

But in your case D is does not have a default constructor so you must explicitly invoke that in any of d1's constructor initializer lists. Or just make D default constructable by giving a default value:


struct d {
int j;

d(int i = 0): j(i) {} // this is now a default constructor.

};

struct d1 : d {
//...
d1(int j) { ... } // implicitly invokes default constructor of d.

};


[Edited by - snk_kid on May 31, 2005 4:36:03 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Andrew Russell
Man...

Here is my best guess at what that jumble of charcters there should have looked like...

*** Source Snippet Removed ***

Now for the love of Pete - go and learn C++ properly (RTFM) and stop spamming the forum!


you don't reply me question.

but give some other knowledge.

snd_kid give the exact answer.

not every newbie could know this.

the key is derived class constructor must call base class constructor.

This is not a spamming thread.

This is a good remind for newbie.

Share this post


Link to post
Share on other sites
so my 2nd question:

if derived class have the same data with base class.how to compiler handle this.

I know if member function have the same name.derived class function will cover base class's.
but what happen for data member.

Share this post


Link to post
Share on other sites
It should be an error, or at the very least, a warning (which should be treated like an error anyway). But for god's sake man, stop with all the freaking threads! This is not a chatsite, and it not meant to be used as such. If you are curious about something, LOOK IT UP, don't spam the forums. And yes, it is spam. Just because it is a valid (if dim) question does not mean it is not spam. 13 valid questions (in 2 days) are still spam. At the very least, stick in the For Beginners forum, because you clearly belong there.

Share this post


Link to post
Share on other sites
Quote:
Original post by Andrew Russell
Or even just try it yourself.

Ahh, but it's easier on the keyboard and head if you look it up first, as opposed to trying, failing, bashing things, pulling hair out, and then looking it up? [grin] Why start at the beginning if you can start at the end?

Share this post


Link to post
Share on other sites
I don't think I've ever seen someone's user rating drop faster in my life...

@derek7
You really need to look for these answers yourself and use these forums as a LAST resort. Its very irritating to see 20 posts by someone in two days, all about questions that could be found and answered by yourself in 10 minutes... you obviously have an internet connection...use it! (Google is an amazing resource... try searching for "Default Constructor and C++" I bet you'll have an answer faster than posting here and waiting...)

No one learns by asking for help for every single little question along the way. You need to investigate these problems yourself because there won't always be someone there to hold your hand while your programming.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!