• 12
• 10
• 10
• 13
• 10

# Base class constructors

This topic is 3052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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 on other sites
post your code. what you're trying to do should be fine. Your code is just wrong.

-me

##### 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 on other sites
Oh so you can't do it from the base class...okay...

##### Share on other sites
Quote:
 Original post by JarwulfIs 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.