Now Reading
EmptyCrate.com: No one Understands C++: Intro

EmptyCrate.com: No one Understands C++: Intro

2023-10-29 04:21:18

That is going to be the primary submit of a number of, although I’m undecided what number of simply but. The title of the submit needs to be self explanatory, however I’ll elaborate. It’s my strongly held perception that C++ is mostly poorly understood and will get a foul rap due to this. It is usually my perception that that is NOT the fault of C++, however reasonably the fault of old-school C++ builders who discovered the language earlier than the language was ratified in 1998.

A fellow Slashdot poster has unknowingly made my level for me fairly nicely, and it’s what I’ll use to begin this intro. The comment in question begins “My tackle C++ is that one of the best packages solely use a fraction of the options.” The commenter additional states that he’s weary of operator overloading and templates.

In my expertise, individuals who make feedback just like the above are usually individuals who assume they’re C++ consultants however who’re really solely novices. I have no idea the commenter personally, so I’m not making an attempt to say something about him particularly.

Additionally, I’m not claiming to be a C++ skilled. I solely have 4 years of expertise with C++, and as we all know, it takes 10 years to turn into an skilled in something. Lets take two quite simple examples. One utilizing a rudimentary understanding of c++ and one utilizing enhance, which makes use of templates behind the scenes for us, and pulling out the large weapons.

Right here is the state of affairs: you have got a callback handler you need to implement in an OOP-ish manner the place members of your class get referred to as when a sure occasion occurs. In our first instance, we solely know the right way to use C model operate tips to implement our code, so we find yourself with one thing like the next:

See Also

class CallbackHandler
{
personal:
  struct CallbackFunction
  {
    CallbackFunction(void (*cbf)(), void *d)
      : cb(cbf), d(knowledge) {}

    void (*cb)();
    void *knowledge;
  }

  vector callbacks;

public:
  void register(void (cb*)(), void *knowledge)
  {
     callbacks
       .push_back(CallbackFunction(cb, knowledge));
  }

  void doCallbacks()
  {
    for (vector::const_iterator
           itr = callbacks.start();
         itr != callbacks.finish();
         itr++)
    {
      itr->cb(itr->knowledge);
    }
  }
};

class ClassWithCallbacks
{
public:
  static void callback1(void *obj)
  {
    reinterpret_cast(obj)
      ->callback1Impl();
  }

  static void callback2(void *obj)
  {
    reinterpret_cast(obj)
      ->callback2Impl();
  }

  void registerSelf(CallbackHandler &cbh)
  {
    cbh.register(&ClassWithCallbacks::callback1,
                 this);
    cbh.register(&ClassWithCallbacks::callback2,
                 this);
  }
personal:
  void callback1Impl() { /* dosomething */ }
  void callback2Impl() { /* dosomething */ }
};

Now, for our second instance, we have now turn into a extra superior consumer. We’ve got discovered enhance and have put to make use of the options of C++ which are “too complicated” as some might say.

class CallbackHandler
{
  vector< enhance::function0 > callbacks;

public:
  void register(enhance::function0 &f)
  {
     callbacks.push_back(f);
  }

  void doCallbacks()
  {
    BOOST_FOREACH(enhance::function0 f, 
                  callbacks)
    {
      f();
    }
  }
};

class ClassWithCallbacks
{
public:
  void registerSelf(CallbackHandler &cbh)
  {
    cbh.register(
      enhance::bind(&ClassWithCallbacks::callback1,
                 this));
    cbh.register(
      enhance::bind(&ClassWithCallbacks::callback2,
                 this));
  }

personal:
  void callback1Impl() { /* dosomething */ }
  void callback2Impl() { /* dosomething */ }
};

Now, I problem anybody to argue that the primary instance is cleaner or simpler to know. You might not perceive the main points of the second instance, however absolutely studying them have to be useful? I’ve LITERALLY seen instances the place a rudimentary understanding of templates, and even the enhance library would lower out AT LEAST 25% of the code written. That a lot of a financial savings should equate to extra maintainable simpler to learn code. Extra to come back on this matter…

Source Link

What's Your Reaction?
Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top