Sign in to follow this  
Jarwulf

Base class constructors

Recommended Posts

Jarwulf    223
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
SiCrane    11839
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
kloffy    1318
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
mattd    1078
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this