default indexed class

Hi guys,

does arduino support a indexed default class?

something like:

class pins
{
      private:
            pin all_pins[MAX_PINS];
      public:
            pin default(int);
};

thanks in advanced

What is pin? all_pins looks like an array, while default looks like a function.

I'm confused by the question.

pin is another class and all_pins in pins class is an array of pins class

the point is to do something like:

class pins
{
      private:
            pin all_pins[MAX_PINS];
      public:
            pin default(int);
};
pin pins::default ( int index )
{
      return all_pins[index];
}

to declare all pins:
pins allpins;

to use it:
allpins[1].<pin_class_properties> …

all_pins in pins class is an array of pins class

No, it isn’t. It’s an array of pin (no s) objects.

to declare all pins:
pins allpins;

allpins is an instance of the pins class. It is not an array of anything, so:

allpins[1].<pin_class_properties> …

is wrong.

If you create an instance of the pins class:
pins myPins;
then, myPins has a member, all_pins, that is an array of pin objects, so you could do this:
myPins.all_pins[14].somePinClassMethod();

Where does the default() method come into play? What does the pins constructor look like? In the example, you haven’t declared one.

well, thanks a lot for your reply. I know [u]some[/u] C and C++ but I don't program for a while now. I understand what you said, and I know I can call it like that. actually I don't, because all_pins is private, but it really doesn't matter, because that was not my question. I just wanted to know if I can use the default feature of the class.

I know I can use: myPins.all_pins[14].somePinClassMethod();

but wouldnt it be easyer to use: myPins[14].somePinClassMethod();

Anyway, nevermind. I'll do it the hard way and move the all_pins to public.

thanks

That is certanly possible! Operator overloading is what it’s called.

I’m too busy to show you, but google it and you’ll find the answer.

I know I can use:
myPins.all_pins[14].somePinClassMethod();

but wouldnt it be easyer to use:
myPins[14].somePinClassMethod();

How could you? myPins would need to be an array of instances of the pins (plural) class, rather than a member that was an array of instances of the pin (singular) class. They are not even close to being the same thing.

class pins {
public:
  pin& operator[] (unsigned i) {return all_pins[i]; }
};


pins myPins;
myPins[14].somePinClassMethod();

I think that that's really stretching the [] operator. Typically, I expect the [] operator to return an object of the same type as it is called from.

But, it is an interesting concept.

When indexing a class called 'pins' I do not find it strange to receive a pin?

If I have int stuff[20]; with values in each position, I expect [] to return an int.

I expect that if I have an array of pins objects, for [] to return a pins. If I have an array of pin objects, I expect [] to return a pin object.

I do not expect pins::[] to return a pin object.

But, that could just be me.

PS. I hate DELL laptop keyboards.

I cant help myself but to answer this :)

I won't bother to put this in code tags but:

int my_pins[10];

clearly indicate a collection of ints. And as such, should return an int when accessed by the index operator.

int pin2 = my_pins[1]; //looks like it works

Now, lets keep this in mind, accessing a collection returns an instance from that collection.

pins my_pins; //what's this? my_pins is not an array, but an object? curious...

pin pin2 = my_pins[1]; //works as well

The ontology is valid. The pins collection still return a pin, what's changed is the type of a pin.

This problem we're discussing now has been discussed up and down by c++ communities. Does operator overloading ever provide an API with unambiguous meaning?

class Array { /implement/ } might be the definite argument for 'yes' but is it better than a get(i) or at(i) even item(i) ?

I like operator overloading, but it should be used with care.