Base class constructors

Started by
4 comments, last by mattd 14 years, 6 months ago
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]
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.
post your code. what you're trying to do should be fine. Your code is just wrong.

-me
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) {}};
Oh so you can't do it from the base class...okay...
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.

This topic is closed to new replies.

Advertisement