5 years ago.

Creating Instances of a Class Within Another Class

Hi Mbed People,

Excuse me if this question seems a little rudimentary, I am relatively new to creating libraries and class structures. I have a piece of working code where one button-press class outputs data using another simple 7-segment display class. I got this to work by creating an instance of the 7-Segment class each time a button is pressed. Although this is operating in the desired manner, it feels like there could be a much more efficient way of doing it.

In buttonPressClass.cpp:

void channelSelect::plusButtonDown(){
    
    currentChannel_ = currentChannel_ + 1;
   
    sevenSeg display(p21,p22,p23,p24,p25,p27,p26); //instance of the sevenSeg Display Class
   
    display.set(currentChannel_);
}

I tried a couple of things, including creating the instance of the 7-segment class globally, but couldn't get it to work.

As I said, the code is working, but any advice that will help me improve my class structures would be greatly appreciated!

Thanks, Tom.

2 Answers

5 years ago.

I'm not sure if I understand you correctly.. But if I'm not mistaken then you are a bit confused about initializing the constructor of classes within classes.

Here is how I would have solved it:

cpp file

channelSelect::channelSelect() : display(p21,p22,p23,p24,p25,p27,p26) { //here we are running the constructor of the display class
    //code here
}

header file

class channelSelect {
public:
    channelSelect();
private:
    sevenSeg display;
}

you could also solve the code this way by putting the pin names in the constructor of the channelSelect-class.

cpp file

channelSelect::channelSelect(PinName pin1, PinName pin2, PinName pin3, PinName pin4, PinName pin5, PinName pin6, PinName pin7) 
        : display(pin1,pin2,pin3,pin4,pin5,pin6,pin7) {
    //code here
}

this way you declare the pins when calling the constructor of the channelSelect-class.

Other than that I think Davids method is a very good suggestion.

Accepted Answer

Hi Tomas,

thanks for your advice. I ended up declaring the pins when calling the constructor of the channelSelect-class.

posted by Tom Wilson 07 Feb 2015
5 years ago.

Hi Tom,

I'm closer to the lower end of the C++ skill set than many others - so I hope to see many responses. That said, I would ponder - does the display class serve any other need? For instance,

  1. Perhaps you have many buttons, but it is never used outside of the "channelSelect" class, or
  2. Perhaps some other methods completely outside of this might also want to access it?

In the first case, I might go with -

class channelSelect {
public:
    channelSelect();
    ~channelSelect();
    void plusButtonDown(void);
    ...
private:
    int currentChannel_;
    sevenSeg display(p21,p22,p23,...);   
    // Having written it, I don't like this here, because the port pin assignments are "buried",
    // and if we pull them out to the channelSelect constructor, then we have two
    // very unrelated concepts (button and display) being bound together in non-obvious ways...
};

channelSelect::channelSelect() {
    currentChannel_ = 2;                 // some default
    display.set(currentChannel_);
}

// but this does mean the constructor runs once, and it simplifies your code to 
void channelSelect::plusButtonDown(){
    currentChannel_ = currentChannel_ + 1;
    display.set(currentChannel_);
}

For the second scenario, then you might pass a reference to channelSelect to the display class constructor.

class channelSelect {
public:
    channelSelect(sevenSeg & display);
    ...
private:
    sevenSeg & display;    // reference instead of pointer [but this is an area where I remain "weak"]
    ...
};

channelSelect::channelSelect(sevenSeg & _display) : display(_display) {
    currentChannel_ = 2;
    display.set(currentChannel_);
}

// and this remains the same...
void channelSelect::plusButtonDown(){
    currentChannel_ = currentChannel_ + 1;
    display.set(currentChannel_);
}

one last disclaimer: I only mentally executed this.

Hi David,

thanks for your advice. I couldn't seem to get the 'reference method' to work. But your other examples really helped my understanding of class structures!

Cheers, Tom

posted by Tom Wilson 07 Feb 2015