Important: Please read the Qt Code of Conduct -

C++ with C standards

  • Hello guys,

    I was building a gui for a simple program a friend made in C++ but I just found out that his program is a cpp file but it is written with the C standards, this means no object orientation and classes.

    I have completely stuck, how can I proceed from now on? I was thinking to convert the program in actual c++ with objects and classes but the whole project is already quite bug with many headers and other .cpp files.

    Another solution I thought is to use the gtk platform to build the gui.

    Any suggestions?

  • Hi,
    It depends what parts are present in your friend's project. If the project only deals with data and has not graphical interface yet, you can just make a C++ wrapper of the useful parts or export them in a library for your graphical interface. If it already has a GUI, you will have to port it to Qt if you want a uniform visual experience.

  • Hey unai_i,

    That sounds very good news. The project mostly deals with data and uses ncurses to make a simple interface. Basically, the useful parts I want from the code are 3 char buffers that have data that I need to display to the gui.

    Do you have any resource where I can check how to make a wrapper or how to export stuff to a library?

  • Basically, if all you need is those 3 char buffers you can wrap the code that generates those in a class and expose your data in methods as QByteArrays or use QBuffers if your data is generated continuously. When you have this, build your interface and pass the data you want to show.

  • It seems that the extern keyword can work also in this case. What do you think about that?

  • Of course, there is not a single way to achieve what you want and the extern keyword can be used too. It all depends how you want to expose your data (through a class, function call, signal slot connection, direct variable or reference access, ...).

  • To answer your question in a generic way, here's how you could think:

    • What does you friend's code do? It probably consumes (receives) data in some way, and produces data in some way.

    • What would be an interface to this functionality that is convenient to you? If this is a small project, an interface could simply be the declaration of a class. What member functions in the class would make it convenient for you to access the functionality that your friend's code implements?

    • Create a class MyEncapsulationOfMyFriendsCode (actually don't - come up with a better name :)) , with the interface that you like, and then use your friends code in the definition of your class by calling it, like C functions, from your C++ class member functions. Start by writing the declaration (the .h header) and start the implementation once you're reasonably satisfied with the interface.

    If you want more information, you can google "c++ c wrapping" or similar.

  • I would wrapped it with some wrappers if and only if I find out I need them(ex : it is no sense to wrapped std::sort, std::find_if and other non OO api by classes).

Log in to reply