Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C how to "hide" macros from main scope

if have a routine like this which I want the main application to access...

    char* get_widget_name(widget_t* w) {
        return name(w);
    }

both the following macro's I want to "hide" while obviously using within get_widget_name

    #define GET_WIDGET(self) (&(self)->base.widget)
    #define name(self)      (GET_WIDGET(self)->name)

I'm basically using unions in structures to "emulate" c++ inheritance in C.

like image 798
Chris Camacho Avatar asked Oct 14 '25 14:10

Chris Camacho


1 Answers

You publish the information to be used in the main application in a header file. In this example, it might be:

#ifndef WIDGET_H_INCLUDED
#define WIDGET_H_INCLUDED

typedef struct widget widget_t;

extern char *get_widget_name(widget_t *w);

#endif /* WIDGET_H_INCLUDED */

And then in the implementation file (widget.c), you define the structure contents and the macros and use them as you see fit, without making them available to the main application at all.

like image 168
Jonathan Leffler Avatar answered Oct 17 '25 02:10

Jonathan Leffler