Author Topic: virtual keyword in derived class (code style guide)  (Read 7280 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6079
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
virtual keyword in derived class (code style guide)
« on: April 16, 2013, 07:10:26 am »
Refer to this: C++ "virtual" keyword for functions in derived classes. Is it necessary? - Stack Overflow
I think this is a good way. But currently it was not in our Coding style - CodeBlocks

Code
Index: plugins/codecompletion/parser/parserthreadedtask.h
===================================================================
--- plugins/codecompletion/parser/parserthreadedtask.h (revision 8991)
+++ plugins/codecompletion/parser/parserthreadedtask.h (working copy)
@@ -17,7 +17,7 @@
 {
 public:
     ParserThreadedTask(Parser* parser, wxMutex& parserCS);
-    int Execute();
+    virtual int Execute();
 
 private:
     Parser*  m_Parser;

Any comments?
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Re: virtual keyword in derived class (code style guide)
« Reply #1 on: April 16, 2013, 09:47:10 am »
Any comments?
Code to the style of the old code at the place you're adding the function and everything will be good :)
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6079
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: virtual keyword in derived class (code style guide)
« Reply #2 on: April 16, 2013, 11:07:03 am »
Any comments?
Code to the style of the old code at the place you're adding the function and everything will be good :)
Oh, I can't understand this sentence, can you be more specific?
I mean, the base class have the style:
Code
class cbThreadedTask
{
  public:
    /// cbThreadedTask ctor
    cbThreadedTask();

    /// cbThreadedTask dtor
    virtual ~cbThreadedTask() = 0;

    /// This function is called to tell the task to abort (check cbThreadPool::AbortAllTasks)
    void Abort();

    /// Override this function with the task's job
    /// Return value doesn't matter
    virtual int Execute() = 0;
So, it is good to preserve the "virtual" before the int Execute() in the derived class. (Although it is not necessary)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13406
    • Travis build status
Re: virtual keyword in derived class (code style guide)
« Reply #3 on: April 16, 2013, 11:46:18 am »
Code to the style of the old code at the place you're adding the function and everything will be good :)
I mean that it is best if you follow the style of the code you're modifying.
If the old code uses virtual ok you use it, too. If not you don't use it.
If you write new code you're free to choose what style to use.

p.s. this is my personal opinion, not a C::B rule
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6079
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: virtual keyword in derived class (code style guide)
« Reply #4 on: April 19, 2013, 03:43:54 am »
I commit the change(rev 8999), because I see a lot of derived classes in C::B use "virtual" keyword for virtual functions.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.