mesos-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Benjamin Hindman (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (MESOS-2756) Update style guide: Avoid object slicing
Date Sat, 20 Jun 2015 17:34:00 GMT

    [ https://issues.apache.org/jira/browse/MESOS-2756?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14594699#comment-14594699
] 

Benjamin Hindman commented on MESOS-2756:
-----------------------------------------

While I agree that we should disallow instantiation of the base class if we don't want people
to be constructing those objects, I don't understand how disallowing instantiation of the
base class helps with object slicing. Making the "disallow construction of base class" modifications
to your example still results in the object slicing problem:

{code:title=ObjectSlicing.cpp|borderStyle=solid}
#include <stdio.h>
#include <vector>

class Base {
protected:
  Base() {}
  Base(int _v) : v(_v) {}
public:
  virtual int get() const { return v; }
protected:
  int v;
};

class Derived : public Base {
public:
  Derived(int _v) : Base(_v) {}
  virtual int get() const { return v + 1; }
};

int main() {
//   Base b(5);                                                                          
                                                                                         
 
  Derived d(5);
  std::vector<Base> vec;
//   vec.push_back(b);                                                                   
                                                                                         
 
  vec.push_back(d);
  for (const auto& v : vec) {
    printf("[%d]\n", v.get());
  }
}
{code}

> Update style guide: Avoid object slicing
> ----------------------------------------
>
>                 Key: MESOS-2756
>                 URL: https://issues.apache.org/jira/browse/MESOS-2756
>             Project: Mesos
>          Issue Type: Improvement
>            Reporter: Joris Van Remoortere
>            Assignee: Joris Van Remoortere
>              Labels: c++
>
> In order to improve the safety of our code base, let's augment the style guide to:
> "Disallow public construction of base classes"
> so that we can avoid the object slicing problem. This is a good pattern to follow in
general as it prevents subtle semantic bugs like the following:
> {code:title=ObjectSlicing.cpp|borderStyle=solid}
> #include <stdio.h>
> #include <vector>
> class Base {
>   public:
>   Base(int _v) : v(_v) {}
>   virtual int get() const { return v; }
>   protected:
>   int v;
> };
> class Derived : public Base {
>   public:
>   Derived(int _v) : Base(_v) {}
>   virtual int get() const { return v + 1; }
> };
> int main() {
>   Base b(5);
>   Derived d(5);
>   std::vector<Base> vec;
>   vec.push_back(b);
>   vec.push_back(d);
>   for (const auto& v : vec) {
>     printf("[%d]\n", v.get());
>   }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message