Custom library question

I know this is probably more of a C++ question, but I have some difficulties writing a library.

How do I construct something in a library?

Here is what I am trying to do.
Rather than rewriting Encoder.h a brilliant library I want to add some options of my own and make a superclass (not sure that is what you'd call it). But i can't seem to construct myEnc

Interface.h

#ifndef Interface_h
#define Interface_h

#include "Arduino.h"
#include <Libraries/Encoder.h>

class Interface
{
	public:
	Interface();
	void begin();
	private:
	Encoder myEnc;
	
	
};

#endif

Interface.cpp

#include "Arduino.h"
#include "Interface.h"
#include <Encoder.h>

Interface::Interface ()
{	
	
}

void Interface::begin()
{
	Encoder myEnc(2, 3);
}

one more little question (or so I hope) is there a way to define the size of an array in the constructor like so :

Interface myInterface=Interface(5);

where an array of int of size 5 would be created i've searched the web for a couple of hours and can't seem to find an answer. What I see most people do is define a constant and use that to define the size of an array… Maybe that is the only way, but it would be lovely if it wasn't.

Thank you very much!

Have a look at how the HardwareSerial class inherit from the Stream class

Or better, look this page :slight_smile:

For your second question, you can allocate memory dynamically, it's just not recommended because of the problems it could create, see this page.

#include <Libraries/Encoder.h>

This is wrong. The path to libraries includes the libraries name. So, unless you have a Libraries folder in the libraries folder, Encoder.h is not going to be found.

class Interface

A class is supposed to represent a real object, like an Encoder. An Interface is NOT a real object. It's a concept.

void Interface::begin()
{
	Encoder myEnc(2, 3);
}

So, myEnc goes out of scope when begin() ends, which it does immediately after creating myEnc. Not very useful.

You need to construct myEnc at the same time you construct your object, NOT in the begin method:

Interface::Interface(int thisPin, int thatPin) : myEnc(thisPin, thatPin)
{
}

Your class doesn't have an array, so answering questions about how to size it is pointless.

Hi thanks for the great replies.

@guix,it wouldn't really be dynamic since I need only create one instance. Or am I wrong?
Also thanks for the link to the C++ Tutorial. I wonder why I haven't looked for one.

@PaulS

This is wrong. The path to libraries includes the libraries name. So, unless you have a Libraries folder in the libraries folder, Encoder.h is not going to be found.

I thought it was wrong too, but it is the only way I can get it working. If I try anything else, the compiler says "Does not name a type"

I assumed it was because the compiler lives one directory higher than my Library folder?

A class is supposed to represent a real object, like an Encoder. An Interface is NOT a real object. It's a concept.

Can you elaborate on this or point me to a site where I can read about this?

Interface::Interface(int thisPin, int thatPin) : myEnc(thisPin, thatPin)
{
}

This does work, but! (I know I'm pushing it)
I rewrote the encoder library it accepts two (being the pins) or three(Pins + a divider) arguments.

Is there a way to circumvent this with an ifdef or something?
Can I make two constructors?

That's why I tried to put it in the begin() part.
I would have two begin parts

void begin()

and
void begin(int pin1, int pin2)

I don't know if I you can understand what I am talking about. This is what you get when you have a non programmer asking programming questions!

Can I make two constructors?

Or three or seven or 22. Well, maybe not 22 if you expect anyone to actually use the library. But you can have more than one constructor.