Jump to content
  • Advertisement
Sign in to follow this  
gameXcore

quick question about inheritance and constructors in c#

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

Hi, i just hit a snag while learning C#, im not really too sure how to explain the problem so ill show what i would have done in C++:
[source language="cpp"]

class class1
{
private:
	int myInt;

public:
	class1(int someInt)
	{
		myInt = someInt;
	}
};

class class2 : public class1
{
public:
	class2() : class1(1);
}


As you can see class 1 has a single constructor that takes an int and stores it, class 2 inherits from class1 but doesnt need to have an int specified when its created because it always uses "1". I cant seem to recreate this in C# though, it wont compile with the error given that there is no overloaded constructor that takes 0 arguments, currently ive hacked around it by including a 0 argument constructor into my base class however there must be a way to do this in c#, can anyone help me out with this? Thanks, Scott (on side question, when people post example classes like those up there, they always call them "foo" and "bar", what exactly do they stand for? =])

Share this post


Link to post
Share on other sites
Advertisement
Use the "base" keyword to call the base class constructor.

class class1 {
private int myInt;

public class1(int someInt){
myInt = someInt;
}
}

class class2 : class1 {

public class2() : base(1){
}
}




Quote:

(on side question, when people post example classes like those up there, they always call them "foo" and "bar", what exactly do they stand for? =])

Metasyntactic Variables

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!