Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Question about class constructors

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

If the constructor is called automatically when an instance of the class is created, how can I pass parameters into the constructor? and what would happen when the instance is first created?

Share this post

Link to post
Share on other sites
You must declare a constructor which takes parameters.

A class may define multiple constructors, differentiated by the types of the arguments - rather like overloading any other member function. When you create a new instance of the class, you must specify which constructor to use, by providing the arguments:

Foo myFoo = new Foo(arg1, arg2); // requires that a matching constructor exists; i.e. arg1 and arg2 are of appropriate types
Bar myBar = new Bar(); // calls "default" no-argument constructor
Baz myBaz = new Baz; // same as above

Share this post

Link to post
Share on other sites

  • 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!