How to name optional arguments in method call?

G’day, Arduino Community.

Just to give you my background, I am a (very) old hand at programming (or, to use that newfangled term, “software development” ;), with older experience in assembler (for various microcontrollers and microprocessors) and C (not C++, though). I have more recent experience in C# (so far, my only exposure to OOP methodology). (Well, to be honest, I have even OLDER experience in writing FORTRAN using key-punched Hollerith cards, but let’s not go there. :wink: ). In the past, I have had some exposure to writing for AVR microcontrollers (in C and assembler), but I am a very new noob to Arduino and C++.

My Question:

So I am writing and testing a C++ hardware device library for Arduino (hopefully, for eventual release to the Arduino community). In my library’s class constructor I have a method with three optional, defaulted parameters. So, in my library’s header file (say, MyDevice.h) I have something like:

...

class MyDevice {
  public:
    // Constructor:
    MyDevice(byte pinNumber = _DVC_DFLT_PIN,
             unsigned long onTime = _DVC_DFLT_ON_TIME,
             unsigned long offTime = _DVC_DFLT_OFF_TIME);

    // No destructor required.

    ... // the rest of the class declaration.

}

... // the rest of the header file.

So now, in the sketch (funny name for a program, but, okay :wink: ), I am trying to figure out how to name optional arguments when I instantiate the class (i.e. in the constructor call), but cannot seem to find out how to do this. In other words, suppose I just want to specify, say, the third argument, and default the first two. How would I do that when I instantiate the class?

In C# syntax, it would look something like this:

...
  // Default the device's pin number and on-time, but
  // set off-time to 5 seconds instead of the default.
  MyDevice myDeviceInstance = new MyDevice(offTime: 5000);
...

Note the named argument, offTime, in the constructor call. How would I do the same kind of thing in Arduino C++?

Thanks and very best regards,
Ed.

I believe the answer is "you can't".

That you need to such a thing indicates your constructor is overly complicated.

Actually, the constructor is very simple. It just sets three private fields, one for each of the parameters. It's just that I want the flexability to default any of those parameters.

I know that, as OOP languages go, C++ is one of the oldest, so it may not have the ability to name arguments in a method call.

If that is the case, I suppose I could just go ahead and write overloads with different method signatures, rather than relying on optional parameters.

Ed.

Stop bragging. I learned to hunt and peck on an IBM 029.

I'm not much into C++ as the overhead is too much but I think what you're trying to do is perhaps better handled by overloading a function rather than optional arguments. As a quick example;

class operation 
{
	public:
	void demo() 
	{
		Serial.println("No Parameter");
	}
	void demo(int x) 
	{
		Serial.print("One Parameter which is x = ");
		Serial.println(x);
	}
	void demo(int x, int y) 
	{
		Serial.print("Two Parameters they are x = ");
		Serial.print(x);
		Serial.print(" and y = ");
		Serial.println(y);
	}
	void demo(double x, double y) 
	{
		Serial.print("Double parameters; x = ");
		Serial.print(x);
		Serial.print(" and y = ");
		Serial.println(y);
	}
};

It'll all sort itself out when the function gets called based on the argument types.

Another thought is to pass a struct that has defaults in place and you only need to diddle those of interest.

Yeah, thanks, DK.

(Is it really bragging when i expose just how old I really am? :wink: )

As I mentioned in my post just before yours, I think overloading si the way I'm gonna go.

I thought of using a struct parameter (or a pointer thereto), but I think that would unnecessarily complicate things for the user/sketch-writer, and would take up a bit more of that oh-so-precious RAM.

So I guess I'll write it with overloads.

(BTW, DKWatson, is your first name Doug and were you once a contractor hired by BNR in Ottawa?)

Thanks, folks.
Ed.

I think I knew Doug, but the D is for David and I didn't like the winters in Ottawa so I went to Winnipeg instead.

DKWatson:
I think I knew Doug, but the D is for David and I didn't like the winters in Ottawa so I went to Winnipeg instead.

Funny.

edpugh:
I know that, as OOP languages go, C++ is one of the oldest, so it may not have the ability to name arguments in a method call.

It is completely impossible. Arguments to a function must be given in exactly the same order as in the function signature, and it is impossible to specify them by name.

For parameters with a default value, they must all be at the end of the function signature, and in the function call will be filled in left-to-right. You cannot skip one.

If you want more flexibility with default values, you can overload the constructor with different parameters, or use static construction functions and give them a descriptive name. In that case, the default values can be specified as private static const member variables so that they are accessible to all the construction functions and don't need to be retyped.