Reputation: 83
I've Google'd "calling base constructor", and I'm not getting the answers I need.
Here's the constructor I have;
public class defaultObject
{
Vector2 position;
float rotation;
Texture2D texture;
public defaultObject(Vector2 nPos, float nRotation, Texture2D nTexture)
{
position = nPos;
rotation = nRotation;
texture = nTexture;
}
}
Now I have that in place, I want to inherit the constructor and all its workings. This is what I'd expect to do;
public class Block : defaultObject
{
// variables inherited from defaultObject
public Block : defaultObject; //calls defaultObject constructor
}
Why can't I do that?
Upvotes: 1
Views: 145
Reputation: 41549
Use : base()
:
public class Block : defaultObject
{
// variables inherited from defaultObject
public Block ()
: base()
{}
}
or with parameters:
public class Block : defaultObject
{
// variables inherited from defaultObject
public Block (Vector2 nPos, float nRotation, Texture2D nTexture)
: base(nPos, nRotation, nTexture)
{}
}
Upvotes: 6
Reputation: 32681
you have multiple problems in your code. It should be like this
public Block(Vector2 nPos, float nRotation, Texture2D nTexture) : base(nPos,nRotation,nTexture) // see the way params are passed to the base constructor
{}
Upvotes: 0
Reputation: 6237
To call the base class's constructor:
public class Block : defaultObject
{
// variables inherited from defaultObject
public Block(npos, rotation, texture) : base(npos, rotation, texture); //calls defaultObject constructor
}
To override the update method, you must declare it virtual in the base class:
public virtual void update() { .. }
Upvotes: 0
Reputation: 56697
Why is it hiding an inherited member?
Because I bet the method in the base class is not marked virtual
.
I see that you removed that part of the question. Well, I've answered it anyway now...
Upvotes: 1