Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[C#] Is it possible to override the default constructor for a struct?

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

If I have a struct:
    public struct Contact2D
    {
        public RigidBody2D[] body;              // Two rigid bodies involved in the collision pair
        public bool closestOnly;                // Only keep the closest shape intersected?
        public Vector2[] point;                 // Contact points for the two shapes involved in collision pair
        public Vector2 normal;                  // Contact normal
        public float penetration;               // Penetration depth (at the contact point)       
        public float t0;                        // Time of first collision (point of entry)
        public float t1;                        // Time of last collision (point of exit)

        float min;                              // Minimum recorded distance for this contact
        float max;                              // Maximum recorded distance for this contact

        public Contact2D(bool closestOnly)
        {
            body = new RigidBody2D[2];
            this.closestOnly = closestOnly;
            point = new Vector2[2];
            normal = Vector2.Zero;
            penetration = 0.0f;
            t0 = float.MinValue;
            t1 = float.MaxValue;
            
            // Reset min/max value for each new contact [Don't use static variable]
            min = float.MaxValue;
            max = float.MinValue;
        }


Is it possible to override the default constructor that creates a new Contact2D but with the bool closestOnly set to true? Therefore: Contact2D contact = new Contact2D() would initialise all the parameters as the Contact2D(bool closestOnly) constructor would, but the closestOnly bool would automatically be set to true? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Don't know about c# in such a case but I would think that you only have to write the constructor yourself with just initializing the attributes you want and leave the remaining untouched. This should pretty much be the same as the default constructor would do.

Share this post


Link to post
Share on other sites
Quote:
Original post by Waterwalker
Don't know about c# in such a case but I would think that you only have to write the constructor yourself with just initializing the attributes you want and leave the remaining untouched. This should pretty much be the same as the default constructor would do.


C# doesn't seem to allow this:

"Structs cannot contain explicit parameterless constructors"

Share this post


Link to post
Share on other sites
Quote:
Original post by Spa8nky
If I have a struct:

*** Source Snippet Removed ***

Is it possible to override the default constructor that creates a new Contact2D but with the bool closestOnly set to true?

You can give explicit values to members:

public struct Contact2D
{
public RigidBody2D[] body; // Two rigid bodies involved in the collision pair
public bool closestOnly = true; // Only keep the closest shape intersected?
public Vector2[] point = new Vector2[2]; // Contact points for the two shapes involved in collision pair
public Vector2 normal; // Contact normal
public float penetration; // Penetration depth (at the contact point)
public float t0 = 42.0f; // Time of first collision (point of entry)
public float t1; // Time of last collision (point of exit)

float min; // Minimum recorded distance for this contact
float max; // Maximum recorded distance for this contact

}



The values are set when the instance is created.

Share this post


Link to post
Share on other sites
Unfortunately, again that is not possible in the world of C#:

"cannot have instance field initializers in structs"

which is why this has me stumped.

Share this post


Link to post
Share on other sites
It's not possible with a struct.

This is due to the nature of structs, in that they are value types rather than reference types.

What is wrong with Contact2D c2 = new Contact2D(true); anyway?

Share this post


Link to post
Share on other sites
No, structs cannot have either initializers for instance fields nor explicit parameter-less constructors. This stems from their nature as value types, which means that the runtime will default initialize a struct to all zeroes.

You can accomplish what you want by using a static construction method:

public static Contact2D ClosestContact()
{
return new Contact2D(true);
}



Looking at the current constructor though, I would say that your class might not be a good candidate for a value type, since many of the members need some sort of initial value other than the default.

Share this post


Link to post
Share on other sites
I don't understand why C# prohibits this. What would break if they allowed you to specify what the 'real' defaults of a struct are, as opposed to assuming that it should always be the default(Type) result?

Admittedly I've never come across a situation where I've needed to - I use classes most of the time - but I've still never read anything which properly explains why it's not allowed.

Hopefully someone can enlighten me?

Share this post


Link to post
Share on other sites
Thanks for the help guys.

I'll stick with explicitely setting the bool each time as it seems like the best option from what you have suggested.

Share this post


Link to post
Share on other sites
Quote:
Original post by Barguast
I don't understand why C# prohibits this. What would break if they allowed you to specify what the 'real' defaults of a struct are, as opposed to assuming that it should always be the default(Type) result?

Admittedly I've never come across a situation where I've needed to - I use classes most of the time - but I've still never read anything which properly explains why it's not allowed.

Hopefully someone can enlighten me?


From what I understand it's an optimization. Structs map easily to blocks of bits. It's relatively quick to simply zero out the block and be good to go.

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!