1

I have to use C++98 for application i am developing for.

I want to control the behavior of constructor based on if it is a first ever call to constructor or not. For first ever constructor call, i want to execute some extra code but it must not execute in consecutive calls. For this I decided to have a static bool that will have default value of false, and after first constructor call, it will be set true for every other call.

class data_sender {
private:
    static bool library_initialized;
public: // etc.
};

This is kept private because i dont want consumer of this class to manipulate this directly. Its not const because obviously i want to change it inside constructor.

Now i cannot assign the default value for this, it says c++ forbids this non-const static variable initialization.

I think my approach is wrong here and i should be using some other method. I am relatively new to c++ so even if you point me to some article or tell me what to search for this.


Another strange thing i noticed is, when i define the library_initialized as false within the .h file,

class data_sender { .... };
bool data_sender::library_initialized = false;

it gives me linker error.

But if i define it inside the data_sender.cpp, it allows me to do this, and compiles without error.

data_sender::data_sender() { ... } //constructor definition
bool data_sender::library_initialized = false; // This is inside data_sender.cpp

This application is developed for mips and ipq architecture in mind, and will be compiled using gcc 4.8 (openwrt-mips) and gcc 5.2 (openwrt-arm) if that matters.


I found one answer on stackoverflow that is similar to mine, why is it required for me to specify this in implementation file only (.cpp) and not in header (.h).

Static constant string (class member)

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 1
    *Now i cannot assign the default value for this, it says c++ forbids this non-const static variable initialization.* Do not reword the error message, post the exact compiler error message and the code where the error raises. – 273K Mar 13 '21 at 05:38
  • The second question has answer here https://stackoverflow.com/questions/3536372/defining-static-members-in-c – 273K Mar 13 '21 at 05:43
  • What exactly is your question? It seems like you've already found your answer but are unwilling to accept it. – JaMiT Mar 13 '21 at 05:46
  • @JaMiT yes i found the solution but i am just consfused, why it compiles fine within cpp file but not in .h file. those file are going to be compiled into a single .o file anyways, what if instead of separate .h and .cpp file i wrote whole definition in .h file... If i remember correctly, university teacher taught me to write all declarations and definition in a single file. But i see modern standards and such always seperate out .h and .cpp files. – Harshal Gohel Mar 13 '21 at 05:57
  • 1
    You **have** to use C++98? I cringe when people say they have to use C++11. I started with C++98, I never want to go back. Forcing a developer to use a 23.5 year old version of the language that's full of mistakes and bad practice (now-a-days) would make me want to strangle someone. – Casey Mar 13 '21 at 06:34
  • @Casey I work with openwrt and qsdk, they use ancient compilers. gcc 4.8. which has incomplete cpp11 support. I cant do anything even if i wanted to. – Harshal Gohel Mar 13 '21 at 06:42
  • @HarshalGohel *"those file are going to be compiled into a single .o file anyways"* -- this is focused on the compilation step, but the error you got occurred during the **linking** step, when multiple `.o` files are combined into one executable. Don't look just at what happens with this source file (`.cpp`); also look at what happens with your other source files. – JaMiT Mar 13 '21 at 16:44
  • Potentially relevant: [Why have header files and .cpp files?](https://stackoverflow.com/questions/333889) and [Why does C++ need a separate header file?](https://stackoverflow.com/questions/1305947) – JaMiT Mar 13 '21 at 16:48
  • @jamit thanks I got my answer why c++ needs separate header file. – Harshal Gohel Mar 14 '21 at 17:03

0 Answers0