Jump to content
  • Advertisement
Sign in to follow this  
SeraphLance

Multiple Types in Constructors?

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

I'm currently writing a noise library, and the other day I stumbled upon JTippetts' ANL in C. I noticed that his functions could take either doubles or other signals as inputs, which seems quite handy to have, rather than all the Flat(someDoubleHere) crap I have right now. Unfortunately, I ran into a bit of a snag trying to implement this.

for mutator functions, this is just a matter of having an overload, but it gets a bit tricky with constructors. For example:


public Blend(Module f1, Module f2, Module control)


I would need 2^3 constructors for this 3-input ctor alone. Some of my other constructors have as many as five inputs, and that's just an ugly mess. Is there any special kind of syntax for this? For example, something like (pseudocode):

Module F1, F2, Control;
double f1, f2, control;
bool m1, m2, m3; //denotes whether F1 or f1 is being 'used'.
public Blend(var f1, var f2, var control)
{
if(f1.getType() == typeof(Module))
{
F1 = f1;
m1 = true;
}
else
{
this.f1 = f1;
m1 = false;
}
. . .
}


I've discovered I could maybe use initializer lists for this purpose with a syntax like the following:


private Module otherVThing;
private double V;
public Object v
{
get{return V;}
set
{
if (value.GetType() == typeof(double))
{
V = (double)value;
}
else if (value.GetType() == typeof(Module))
{
otherVThing = (Module)value;
}
}
}

...

Foo a = new Foo{ arg1 = 4, arg2 = someModuleThingy, ... }


but this is a bit obtuse (if it works at all) and a massive amount of work for my existing codebase. Is there any easy way to get around this or should I just grin and bear it?

Share this post


Link to post
Share on other sites
Advertisement
Can't you just wrap doubles into Modules and/or provide implicit casts? That way you could treat everything as modules..

Share this post


Link to post
Share on other sites
First off... no you don't. You require MAYBE two constructors. Having one for each possible combination of doubles and Module is...to put it plainly: REALLY STUPID. Have one that takes just doubles, and one that takes just modules.

Share this post


Link to post
Share on other sites

First off... no you don't. You require MAYBE two constructors. Having one for each possible combination of doubles and Module is...to put it plainly: REALLY STUPID. Have one that takes just doubles, and one that takes just modules.


What if I wanted to create Blend(0.0, someFBM, someFunc) as per the header above?

Honestly, both the constructor explosion and the property feng shui are pretty much completely insane as far as I can tell, so I don't plan on doing either of them. However, the above syntax is pretty valuable to have for various reasons, so I don't want to entirely forsake it either.

Share this post


Link to post
Share on other sites
Again, just create a DoubleModule which always returns the double. Look at this:

Blend((DoubleModule)0.0, someFBM, someFunc)

and then imagine writing a single constructor and single functions/methods/whatever, since you only need to handle the case where there are modules. You may not even need that cast, depending on implementation.

Share this post


Link to post
Share on other sites

Again, just create a DoubleModule which always returns the double. Look at this:

Blend((DoubleModule)0.0, someFBM, someFunc)

and then imagine writing a single constructor and single functions/methods/whatever, since you only need to handle the case where there are modules. You may not even need that cast, depending on implementation.


Thanks. This led me to discover the syntax for user-defined casts. I have a "DoubleModule" already, that I call Flat. However, I'd like to be able to cast this implicitly. Unfortunately, it doesn't seem like I can do this, as implicit casts aren't allowed in interfaces.

If they were, I could have just done this:
public interface Module
{
...
public static implicit operator Module(double value)
{
return new Flat(value);
}
}

Share this post


Link to post
Share on other sites
Ahh. No, they aren't.. Perhaps it would have been possible if you used a base class instead.. I'm not sure if a cast to a base class can return a derived class.

Still, that cast simply has to beat the alternative.

BTW, since you are using C#, you might want to prefix your interfaces with an 'I', since that's the usual notation (google 'MSDN Design guidelines for class libraries').

Share this post


Link to post
Share on other sites

Ahh. No, they aren't.. Perhaps it would have been possible if you used a base class instead.. I'm not sure if a cast to a base class can return a derived class.

Still, that cast simply has to beat the alternative.

BTW, since you are using C#, you might want to prefix your interfaces with an 'I', since that's the usual notation (google 'MSDN Design guidelines for class libraries').


Module exists solely for the purposes of polymorphism, so changing to a base class went off without a hitch. Casting works fine too. LuaInterface doesn't like it, but I should be able to figure it out from here. Thanks a bunch.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!