How to do not use static function in class?

Code

The code block(s) should be between ```c and ``` tags:

---------------------------------------------------------------------------------------------------
<main.cpp>
---------------------------------------------------------------------------------------------------
#include "B.hpp"
int main(int argc, char ** argv)
{
B base;
base.b_action();
}
---------------------------------------------------------------------------------------------------
<B.hpp>
---------------------------------------------------------------------------------------------------
#ifndef B_H
#define B_H

/*********************
 *      INCLUDES
 *********************/

#include <iostream> // for std

#ifdef LV_CONF_INCLUDE_SIMPLE
#include "lvgl.h"
#include "lv_ex_conf.h"
#else
#include "lvgl/lvgl.h"
#include "lv_ex_conf.h"
#endif

#include "A.hpp"

namespace screens
{
  class B
  {
  public:
    B();
    void b_action();
    
  private:
    void create_b_scr();
  };
}// namespace screens

#endif /*B_H*/
---------------------------------------------------------------------------------------------------
<B.cpp>
---------------------------------------------------------------------------------------------------
#include "B.hpp"

namespace screens
{
  B::B(){}

  /**
   * @brief : Called by main.cpp
  */
  void B::b_action()
  {
    create_b_scr();
  }

  /**
   * @brief : Create Manual Cleaning Screen
   * @return : return nothing
  */
  void B::create_b_scr()
  {
    /* create screen */
    lv_obj_t * b_scr = lv_obj_create(NULL, NULL);

    /*Create [label] style*/
    static lv_style_t style_txt;
    lv_style_copy(&style_txt, &lv_style_plain);
    style_txt.text.font = &lv_font_roboto_28;
    style_txt.text.letter_space = 2;
    style_txt.text.line_space = 1;

    /* img_system button */
    lv_obj_t * img_v_btn = lv_imgbtn_create(b_scr, NULL);
    lv_imgbtn_set_src(img_v_btn, LV_BTN_STATE_REL, &img_main_v_btn_REL);
    lv_imgbtn_set_src(img_v_btn, LV_BTN_STATE_PR, &img_main_v_btn_PRE);
    lv_imgbtn_set_src(img_v_btn, LV_BTN_STATE_TGL_REL, &img_main_v_btn_PRE);
    lv_imgbtn_set_src(img_v_btn, LV_BTN_STATE_TGL_PR, &img_main_v_btn_PRE);
    A a_base;
    lv_obj_set_event_cb(img_v_btn, a_base.go_a_btn_clicked);
    lv_obj_set_width(img_v_btn, 200);
    lv_obj_set_height(img_v_btn, 200);
    lv_obj_align(img_v_btn, NULL, LV_ALIGN_IN_TOP_LEFT, 180, 200);
    lv_imgbtn_set_toggle(img_v_btn, true);
    /* img_v label */
    lv_obj_t * v_label = lv_label_create(b_scr, NULL);
    lv_label_set_text(v_label, "v_btn");
    lv_obj_set_style(v_label, &style_txt);
    lv_obj_align(v_label, img_v_btn, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
  }
}// namespace screens
---------------------------------------------------------------------------------------------------
<A.hpp>
---------------------------------------------------------------------------------------------------
#ifndef A_H
#define A_H

/*********************
 *      INCLUDES
 *********************/

#include <iostream>              // for std

#ifdef LV_CONF_INCLUDE_SIMPLE
#include "lvgl.h"
#include "lv_ex_conf.h"
#else
#include "../../../lvgl/lvgl.h"
#include "../../../lv_ex_conf.h"
#endif

namespace screens
{
  class A
  {
  public:
    A();
    void a_go_action();
    void go_a_btn_clicked(lv_obj_t *obj, lv_event_t event);

  private:
    void create_a_scr();
    
  };
  
} // namespace screens
#endif /*A_H*/
---------------------------------------------------------------------------------------------------
<A.cpp>
---------------------------------------------------------------------------------------------------
#include "A.hpp"

namespace screens
{
/**
 * @brief : Initialize
*/
A::A(){}

/**
 * @brief : 
*/
void A::a_go_action()
{
  create_a_scr();
}

/**
 * @brief :
*/
void A::create_a_scr()
{
  lv_obj_t * a_scr = lv_obj_create(NULL, NULL);

  /*Create [label] style*/
  static lv_style_t style_txt;
  lv_style_copy(&style_txt, &lv_style_plain);
  style_txt.text.font = &lv_font_roboto_28;
  style_txt.text.letter_space = 2;
  style_txt.text.line_space = 1;


  lv_disp_load_scr(a_scr);
}

/**
 * @brief : It's button action.
*/
void A::go_a_btn_clicked(lv_obj_t *obj, lv_event_t event)
{
  if (event == LV_EVENT_CLICKED)
  {
    // a_go_action();
    std::cout << "[HI LOG] GO A BTN PRESSED!" << std::endl;
  }
}
}//namespace screens
---------------------------------------------------------------------------------------------------

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

A class’s [go_b_btn_clicked] function is called as B class’s action.
i want to [go_b_btn_clicked] make as non-static function. i got a reply about my wonder from littlevgl github.(https://github.com/littlevgl/lvgl/issues/1310#event-2881436703) but i think im going wrong… how can i remove static in my class?

i know if [go_b_btn_clicked] function defined outside of the class, they could use. but i want to know the way use in class without static. help me …!

This is a basic C++ issue, not a LittlevGL issue. In many C++ implementations, class methods are always passed an extra, hidden first parameter (which is referenced as this inside your code). LittlevGL, as a C library, doesn’t handle that nicely. The solution is to create a separate static function that knows how to invoke the method within your class.

If you need more information, this might help: https://stackoverflow.com/questions/31018005/c-member-function-as-callback-function-to-external-library

1 Like

ok, i got it. for your answer, i got less pressure using static in the classes. thank you!