Go Down

Topic: Best way to reference another class library (Read 1 time) previous topic - next topic

Fredrik

Hello

I'm a c# guy, so I need some opinions on how to best accomplish this in arduino c/c++:

I have a class library (is that the correct term? A .h and .cpp file that defines a class). Let's call it ClassA. And then I have another class library (from an external part), lets call it ClassB.

ClassA is to use some methods in ClassB in a few places.

What I've done now is, in ClassA i included ClassB.h, and in the constructor of ClassA I pass an instance of ClassB that is stored as a private variable in ClassA. Like so:

Code: [Select]
ClassA.h:
<include ClassB.h>
private:
   ClassB _b

ClassA.cpp:
<include ClassB.h>
ClassA::ClassA(ClassB b)
{
   _b = b;
}
ClassA::MethodX()
{
   _b.doSomething(...);
}


Is this a good way of doing it? Will _b in ClassA be a reference to the instance of ClassB that is passed?

Is there a better way of doing this?

I'm thinking it would be neater to pass a reference to the method of ClassA, instead of the class, so then ClassA could be any method of any object that does the work I want... And then ClassA wouldn't need to have a reference to ClassB. But I have not managed to figure out how to do this, pass a reference and store the reference to a method in a private variable, there's too much * and & and () going on...

Thanks!

Nick Gammon


Is this a good way of doing it?


No.


Will _b in ClassA be a reference to the instance of ClassB that is passed?


No, because you haven't declared the variable as a reference (that is there is no "&" symbol there). It will be a copy, and how good a copy depends on your copy constructor, or your operator=.


Is there a better way of doing this?


Almost certainly. What are you ultimately trying to achieve here?

You might want to check this out:

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14

Fredrik

Haha, ok then!

Quote
What are you ultimately trying to achieve here?
XD

Ok, let's be a little more straightforward.

I am using a shield that handles more pins than the regular arduino, so instead of the regular globally available digitalRead() I have to call XX.digitalRead(), a method of instance of the class that handles the pins for my shield.

I am developing some classes of my own to use, for example a Button class, that takes care of the state of the buttons for the pins of the shield, debouncing and stuff. So, I want to create an amount of instances of my button class, one for different pins, and then I want the button to read the pin, which has to be done via a call to XX.digitalRead().

So, that's what I'm trying to achieve.

So I realized i could use the keyword "extern XX" to achieve the same thing as the globally available digitalRead(), but that's a bit too ugly I think.

PaulS

Quote
So I realized i could use the keyword "extern XX" to achieve the same thing as the globally available digitalRead(), but that's a bit too ugly I think.

If XX is an instance of a class, that probably won't work, anyway.

Enough of the handwaving. Post your actual code, and we'll help you modify it is pass things properly by reference.

Fredrik

Quote
If XX is an instance of a class, that probably won't work, anyway.


I tried it out, it did indeed work!

Thanks, ok, I'll try to get it sorted out a bit and post something.

I just thought this would be like a very obvious thing to do... one class that uses functionality provided by another... But maybe I'm thinking the wrong way.

Thanks a lot for helping me out guys.

Go Up