4

I programmed a lot C in the past, not so much OOP so far. My question is about how to use objects properly, in an OOP way. First example, lets say we receive UDP messages and swap them from little to big endian. In the example below, the methods from the BinarySwapper object are more used in a functional programming way than in a OOP.

My question is if the methods of BinarySwapper should be static, because they do not have to be necessarily methods of that instance. Or is this even the right way to code OOP?

int main() {

WSASession session;
UDPSocket socket;
socket.bind(PORT);
BinarySwapper swapper;

while (true) {

    char buffer[1024];
    socket.recv(buffer);
    swapper.swap(buffer);

    // ... continue
}
} 

My second question is similar. Lets take the following example. The char buffer just gets edited in the objects, at the end I send it.

int main() {

WSASession session;
UDPSocket socket;
socket.bind(PORT);

Message msg;
FormattedMessage formMsg;

while (true) {

    char buffer[1024];
    socket.recv(buffer);
    msg.decode(buffer);
    formMsg.format(buffer);
    socket.send(buffer);
    // ... continue
}
}

Is this a bad style? If yes, why? Is OOP more about inheritance and so on or also about to create and destroy objects "all the time"? I could also program it like this, but it look less efficient in terms of memory and runtime. Thanks for your help.

while (true) {

    Message msg = socket.recv();
    msg.decode();
    FormattedMessage formMsg(msg);
    fromMsg.format();
    socket.send(formMsg);
    // ... continue
}
muella91
  • 189
  • 1
  • 2
  • 12
  • 6
    my 2 cents: Pure OO code is terrible imho. C++ is a multi-paradigm language, so you dont have to be afraid to mix functional with OO style – 463035818_is_not_an_ai Feb 11 '20 at 17:18
  • 1
    There is no problem (IMHO) in defining a class with static member functions. Indeed, I use this paradigm a lot, with classes that *only* have static functions - as a sort of 'local library' class; others would prefer using a namespace in such cases, but it's up to you. – Adrian Mole Feb 11 '20 at 17:23
  • 1
    You can and have to use all C++ tools. OOP solves problems, but not all. – Michael Chourdakis Feb 11 '20 at 17:23
  • 1
    I am just a little bit curious: how do you convert endianness without knowing the type boundaries (i.e. tell whether this is a start of an u16 or u32)? – ph3rin Feb 11 '20 at 17:23
  • 2
    Inheritance is a neat and very useful tricks, but it often has hidden costs. C++ has its own idioms that are apart from OOP. For example, [RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) is one of the most important and not taught nearly enough. Get yourself some of the [intermediate C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take a look at the [C++ Core Guidelines](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) – user4581301 Feb 11 '20 at 17:27
  • 1
    Directly related to the question is this guideline: [C.4: Make a function a member only if it needs direct access to the representation of a class](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c4-make-a-function-a-member-only-if-it-needs-direct-access-to-the-representation-of-a-class) – user4581301 Feb 11 '20 at 17:30
  • @KaenbyouRin These was just a simplified version, of course I pass the array length and I also have different functions for different data types. – muella91 Feb 11 '20 at 17:33

0 Answers0