0

I got stuck with c++. I do Java since more than 15 years, but c++ since a couple of weeks only. So, doing c++ is influenced by Java, no question! But as c++17 goes into this direction too, it may not be completely false. Here is my case: I need a shared library containing a class called MyListener abstracted as:

    class MyListener {
      private:
        static int field1;
        static struct field2;
      public:
        ~MyListener() {
         // do whatever!
        }
       //constructor
       MyListener() {
         field1 = 10;
         field2.name = “Hello”;
         field2.addr=”Dolly”;
       }
       Vector<char> getMessage() {
          Open socket using initialized static fields;
          return socket.message;
       }
    };

And I need a Test class, to use the shared library, let’s say MyListenerTest.cpp containing something like:

    int main() {
       MyListener * ml = new MyListener();
       while(true){
         std::vector<char> data = ml->getMessage();
       }
       delete ml;
    }

Question:

  • The Class MyListener can be compiled and a shared library like libMyListener.so is created.

To compile the Test class I defined something like

    g++ -std=c++14 -LpathtoMyListener –lMyListener -etc

But the class MyListener can not be seen from MyListenerTest.cpp How to tell MyListenerTest where to look for? Of course, first I declared the class in a header file and included this file in the test program, so the class is seen. But either I get a defined twice error, or if I declare the class in the header only, I get an error w.r.t the private fields not being visible. Of course I tried also to use setters and getters as it’s common use in Java. But this gave a horrible solution!

  • How to handle the case in a clean way with c++?

At the end the shared library shall be accessed from Java through JNI. For now I stick to c++ for test. Working on Debian9 and Eclipse Oxigene.

Pirate
  • 2,886
  • 4
  • 24
  • 42
juerg
  • 381
  • 4
  • 18
  • Unrelated tip, use `new` sparingly. When you don't need your variable on the heap, it's often best to just create it on the stack. This is not Java and we don't have an GC cleaning up. – Jasper Dec 28 '17 at 09:31
  • Regarding `defined twice` error, you need to use include guards in your header file, either `#pragma once` as a every first line of the header file or `#ifndef/#define/#endif` https://en.wikipedia.org/wiki/Include_guard – igagis Dec 28 '17 at 09:33
  • 2
    C++ is **not** Java and you should get rid of any analogies between the two and start with a clean slate. Then read these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Dec 28 '17 at 09:34
  • C++ is a scaled down version of god. Java is for quiche eaters and for girls. That's the chief difference. – Bathsheba Dec 28 '17 at 09:36
  • Have you included a header with the definition fom `MyListener` class in `MyListenerTest`? – Serge Ballesta Dec 28 '17 at 09:40
  • 2
    @Bathsheba - Hey, what's wrong with eating quiche?! – StoryTeller - Unslander Monica Dec 28 '17 at 09:49
  • 1
    @StoryTeller um... And what's wrong with girls? Hah! – Quentin Dec 28 '17 at 10:28
  • @Quentin - Must say I realized what trap you laid for me too late. Well played, sir. Well played. – StoryTeller - Unslander Monica Dec 28 '17 at 10:34
  • @StoryTeller but the trap is that of the cat in the hat. I'm just a passer-by. – Quentin Dec 28 '17 at 10:40
  • 1
    @Quentin - A conspiracy. I knew it! – StoryTeller - Unslander Monica Dec 28 '17 at 10:42
  • @Quentin uhh.. I don't? – Jasper Dec 28 '17 at 10:47
  • @Quentin I think you meant to tag juerg in your first comment – Jasper Dec 28 '17 at 11:52
  • @Jasper whoops, I did! Saw only one @-autocomplete and figured it was the right one. Sorry! – Quentin Dec 28 '17 at 11:53
  • Declaring the class in the header **only** seems like the right approach. Why you declare the fields static I don't understand. Some other Java-trick? Also in C++ there is no requirement to put everything in a class, separate variables and free functions are totally acceptable. – Bo Persson Dec 28 '17 at 12:06
  • @Jasper, I know. But stack space is limited also. Finally I need a singleton, living a very long time on the heap. This is also the idea behind the static fields. The socket must be initialized once and then should be available for each message receive or send over the socket. – juerg Jan 02 '18 at 12:05
  • @Ballesta. Of course I did. I'm new to c++, but new on programming. – juerg Jan 02 '18 at 12:06
  • @Bo Persson. The question is how detailed must the declaration of the class be in the header file. I hoped to hear some thing like: you declare the public methods access in the header file and the private fields and there initialization in the implementation file. I don't know if this can be done. and how it's done best. – juerg Jan 02 '18 at 12:10
  • @all others, I hope you passed a good time on new Years Eve and New Years Day. I mean much better than the quality of comments. Just to remember my question has nothing to do with a childish discussion of C++ much better than Java, or vice versa. – juerg Jan 02 '18 at 12:21
  • @juerg - No, you can't separate the different parts. A class declaration must be complete, and all in one place. You can possibly have some free functions as implementation helpers in the .cpp file (so you don't have to put all of them in the private section). But that's about it. On the other hand, unlike Java (sorry!) you can have free functions without a class. That can be useful if you provide some service that can't have several instances anyway. – Bo Persson Jan 02 '18 at 12:21
  • @ Bo Persson Yes I know about free functions. The question is, is this the only way to do it? Of course, comming from Java, a class seams to be a natural choice. I'm not borned in a way to see this as the only solution. Much more important to me is the idea of a singloton initialized once at startup and living until shut down of the program. But even this is not a hard requirement. The goal is to have the read and write access to the socket as short as possible between following accesses. Keeping the data on the Java side works, but is very logger technique! – juerg Jan 02 '18 at 12:40

1 Answers1

0

Got it! The answer is not to use "static" modifier for the class fields. Doing so I got what I want. Private fields with the socket data initialized trough the constructor and the methods to read or to write messages through the socket using the socket initialization within the private fields. Now, we must just assure to have only one copy of the class object. But as it is an embedded system, this is the easy part. Yes, in Java I use often static classes with static methods to have specialized types without creating an object. May be possible with c++ too, I just don't know it.

juerg
  • 381
  • 4
  • 18