1

I'm developing for an embedded hardware using C++ and I often use pointers to const (ROM) structs to minimize the object sizes.

When I get a pointer passed to my class constructor, I want the compiler to create another ROM object based on the one I passed but with one or two arguments changed and just then call the parent class constructor (Display_Element). Any ideas of how this could be done?

Since a const string can be declared within a parameter list I though possibly there could be a way of also declaring a const struct within a parameter list.

I wanted to do something like this, (which is illegal in C++)

Button::Button(const Colors_const* ecc, const Display_element_const* edc, const Element_const* eec, char* elabel,
    Display_element* eparent, Screen* escreen) :
      Display_element(ecc, cc,Display_element_const {
                        edc->xi+200,
                        edc->xf,
                        edc->yi,
                        edc->yf,
                        edc->font_size,
                        edc->image,
                        edc->image_x_offset,
                        edc->image_y_offset,
                        edc->label_x_offset,
                        edc->label_y_offset,
                        edc->mouse_down_ptr,
                        NULL,
                        edc->largura_borda_externa,
                        edc->borda_panel
                    },
                    eec,elabel,eparent,escreen) {

    flags.largura_borda = 2;
    //flags.largura_borda_externa = 3;
    flags.borda_light_shadow = true;
    flags.sliding_text = true;
    flags.dont_paint_on_click = false;
}

Thanks

  • That syntax could be valid if you take out the stray `;` after `edc->borda_panel`. But it will create a temporary struct at run time, not a ROM struct at compile time. – aschepler Mar 01 '13 at 19:30
  • Any idea of how to create it on ROM at compile time. I mean, just like a fn(...,"The brown dog",...) would do with the string? – Douglas Rocha Ferraz Mar 01 '13 at 19:38
  • 2
    The `constexpr` keyword (new to C++11) can make it possible to put objects in ROM. But of course all your inputs must be known at compile time. Maybe if `edc` were a template parameter instead of a function parameter. – aschepler Mar 01 '13 at 19:43
  • Actually, even without the `;` my compiler (IAR for ARM) throws `Error[Pe254]: type name is not allowed C:\Users\Douglas\Documents\nox\controller_components_test.cpp` – Douglas Rocha Ferraz Mar 01 '13 at 19:44

1 Answers1

0

Well, it seems what I want to do is really illegal and can't be done in C. But philosophically I keep asking myself: if I can allocate a const char[n] written inside a parameter list such as fn(...,"The brow dog",...) why not a way to allocate a const struct the same way? If someone knows the answer, please post!

The workaround I found is to do it the canonical way: declare a const struct and then later assign the appropriate pointer to the struct (something I wanted to be done inside Display_element function on the first place). It solves my problem, but not the conceptual question I've been trying to formulate...

const Display_element_const new_ec = {
        edc->xi+200,
        edc->xf,
        edc->yi,
        edc->yf,
        edc->font_size,
        edc->image,
        edc->image_x_offset,
        edc->image_y_offset,
        edc->label_x_offset,
        edc->label_y_offset,
        edc->mouse_down_ptr,
        NULL,
        edc->largura_borda_externa,
        edc->borda_panel
    };


Button::Button(const Colors_const* ecc, const Display_element_const* new_edc, const Element_const* eec, char* elabel,
    Display_element* eparent, Screen* escreen) :
      Display_element(ecc, edc,eec,elabel,eparent,escreen) {
    //previously dc = edc, assigned inside Display_element fn
    dc = &new_ec;