Jump to content
  • Advertisement

Archived

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

Question about class constructors

This topic is 5125 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
Advertisement
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.

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

Sign me up!