Jump to content
  • Advertisement
Sign in to follow this  
Jarwulf

Base class constructors

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

Is it possible to inline constructors into base classes? I tried to do it for one of my base classes so I could initialize values for my objects like this
class base
{
float startpositionx,startpositiony,bufferx,buffery;
     public:
base();//constructors 
base::base(float sx,float sy,float bx,float by) {
  startpositionx = sx;
  startpositiony = sy;
  bufferx = bx;
  buffery = by;
}

};


class obj:public base
{
};


main()
{
obj object(x,y,z,a); 
}

but it gave me an error saying no overloaded function can take 4 arguments. Then when I tried to do constructors for the derived classes individually it gave me a linking error. EDIT: Sorry this is C++ [Edited by - Jarwulf on November 10, 2009 7:51:49 PM]

Share this post


Link to post
Share on other sites
Advertisement
You might want to mention basic information like what programming language you're asking about. Also, be more explicit about what you're trying to accomplish. I can think of more than a few things that you could mean that would give you problems. More code for what you're trying wouldn't hurt.

Share this post


Link to post
Share on other sites
In C++0x:
class obj:public base
{
public:
using base::base;
};

Otherwise:
class obj:public base
{
public:
obj(float sx,float sy,float bx,float by):base(sx,sy,bx,by) {}
};

Share this post


Link to post
Share on other sites
Quote:
Original post by Jarwulf
Is it possible to inline constructors into base classes?

Sure, but this isn't what you're looking for.

Quote:
but it gave me an error saying no overloaded function can take 4 arguments. Then when I tried to do constructors for the derived classes individually it gave me a linking error.

In C++, constructors are not inherited, and the default behaviour is to call the superclass's nullary constructor. If you want to use another one, you need to explicitly call that superclass constructor (with appropriate arguments) via the subclass's constructor's initialization list.

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!