cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing a protected member in derived class

Ezgi
Senior

I have a circle of varying width. I created a subclass that was inherited by Screen1ViewBase class. I want to access protected members of ScreenViewBase.

Screen1ViewBase;

#ifndef SCREEN1VIEWBASE_HPP
#define SCREEN1VIEWBASE_HPP
 
#include <gui/common/FrontendApplication.hpp>
#include <mvp/View.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
#include <touchgfx/widgets/Box.hpp>
#include <touchgfx/widgets/canvas/Circle.hpp>
#include <touchgfx/widgets/canvas/PainterRGB565.hpp>
 
class Screen1ViewBase : public touchgfx::View<Screen1Presenter>
{
public:
    Screen1ViewBase();
    virtual ~Screen1ViewBase() {}
    virtual void setupScreen();
 
protected:
    FrontendApplication& application() {
        return *static_cast<FrontendApplication*>(touchgfx::Application::getInstance());
    }
 
    /*
     * Member Declarations
     */
    touchgfx::Box __background;
    touchgfx::Box box1;
    touchgfx::Circle circlewidth;
    touchgfx::PainterRGB565 circlewidthPainter;
 
private:
 
    /*
     * Canvas Buffer Size
     */
    static const uint16_t CANVAS_BUFFER_SIZE = 7200;
    uint8_t canvasBuffer[CANVAS_BUFFER_SIZE];
};
 
#endif // SCREEN1VIEWBASE_HPP

Screen1View.cpp;

#include <gui/screen1_screen/Screen1View.hpp>
#include<gui/WidthChange.hpp>
 
Screen1View::Screen1View()
{
 
}
 
void Screen1View::setupScreen()
{
    Screen1ViewBase::setupScreen();
}
 
void Screen1View::tearDownScreen()
{
    Screen1ViewBase::tearDownScreen();
}
void Screen1View::handleTickEvent()
{
	WidthChange::Change();
}

This is the header of my class;

#ifndef WIDTHCHANGE_HPP    
#define WIDTHCHANGE_HPP 
 
#include <gui/screen1_screen/Screen1View.hpp>
#include <gui_generated/screen1_screen/Screen1ViewBase.hpp>
 
class WidthChange : public Screen1ViewBase
{
 
public:
 
	static void Change();
 
};
 
#endif

and cpp;

#include <gui/WidthChange.hpp>
#include <gui_generated/screen1_screen/Screen1ViewBase.hpp>
#include <gui/screen1_screen/Screen1View.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
 
void WidthChange::Change()
{
	float currentRad;
	float lineWidth;
	
	circlewidth.invalidate();
 
	circlewidth.getRadius(currentRad);
	if (currentRad >= lineWidthLimit)
	{
		circleWidthChangeFactor = -0.25;
	}
	else if (currentRad<= 15)
	{
		circleWidthChangeFactor = 0.25;
	}
	currentRad = currentRad + circleWidthChangeFactor;
 
	circlewidth.setRadius(currentRad);
	circlewidth.getLineWidth(lineWidth);
	circlewidth.setLineWidth(lineWidth + 2 * (circleWidthChangeFactor * (-1)));
 
	circlewidth.invalidate();
	}

Screen1View can access the protected members of Screen1ViewBase. Why can't I access them?

3 REPLIES 3
Michael K
Senior III

You are trying to use a static functions on non-static objects. This is not possible - all variables used in static functions must themselves be static (or local to that function). A static variable, like a static function, can exist without being a member of another object. If you want to act on a non-static (specific) object, such as circlewidth (which is a member of Screen1ViewBase) in a static function, you must take a reference to the object as a parameter, and perform your actions on that reference.

Between this question and your previous few, I recommend you go back and review more basic OOP and C++ concepts such as static classes/variable, inheritance etc - these issues are not TouchGFX specific and should not be asked here.

Is there a reason you can't just write your Change() function in your Screen1View.cpp?

Embedded UI/UX Consulting: cadenza.design

I want my code to be portable. If touchgfx is updated, I don't want to change my whole code.

When you update the TouchGFX designer, nothing in the GUI folder gets changed. Only the code in the generated ​folder. You can safely write your code in Model.cpp, ScreenXView.cpp and ScreenXPresenter.cpp (among others) without it ever being touched by the designer or cubemx. Don't change the Screen1ViewBase.cpp though because that gets overwritten everytime you generate code.

I've been working with TouchGFX for over a year and have upgraded 4 times (1.12-1.16) and have not had issues with the code I've written in the derived class. That's why they've designed it this way.​

Embedded UI/UX Consulting: cadenza.design