JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++


LANG.CAST.OBJSLICE : Object Slicing

要旨

A value of class type C is assigned to a variable of type B, where B is a base class of C.

In such an assignment, any additional member data that is present in class C but not in class B is not copied into the variable. The resolutions of any subsequent method calls on the variable may also be confusing for human readers.

Use configuration file parameter OBJSLICE_WARN_NEW_MEMBER_ONLY=Yes to specifies whether or not warnings should be restricted to the case where class C contains data members that are not present in class B (and so will be "sliced off" in the assignment).

プロパティ

クラス名 Object Slicing
日本語クラス名 Object Slicing
クラス分類 スタイル (style)
ニーモニック LANG.CAST.OBJSLICE
カテゴリー
AUTOSARC++14 AUTOSARC++14:A12-8-6 Copy and move constructors and copy assignment and move assignment operators shall be declared protected or defined "=delete" in base class.
  AUTOSARC++14:A15-3-5 A class type exception shall be caught by reference or const reference.
MisraC++2008 MisraC++2008:12-8-2 The copy assignment operator shall be declared protected or private in an abstract class.
  MisraC++2008:15-3-5 A class type exception shall always be caught by reference.
MisraC++2023 MisraC++2023:18.3.2 An exception of class type shall be caught by const reference or reference
CERT-CPP CERT-CPP:OOP51-CPP Do not slice derived objects
JSF++ JSF++:96 Arrays shall not be treated polymorphically.
  JSF++:183 Every possible measure should be taken to avoid type casting.
対応言語 C++ のみ利用可能です。 C は利用できません。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Object Slicing"

namespace lang_cast_objslice {
  static int g = 0;

  class Base {
  public:
    Base(){b=++g;}
    int b;
    void f(void){}
  };

  class Derived: public Base {
  public:
    Derived(){d=1;};
    int d;
    void g(void){}
  };

  class DerivedNoFields: public Base {};

  void useBase(Base inb){}

  void makeAssignments(void){
    Base baseobj;
    Derived derivedobj;
    DerivedNoFields derivednofieldsobj;

    useBase(baseobj);                        // ok: baseobj is a Base
    useBase(derivedobj);         // 'Object Slicing' warning issued here
    useBase(derivednofieldsobj); // 'Object Slicing' warning issued here only if OBJSLICE_WARN_NEW_MEMBER_ONLY=No

    baseobj=derivedobj;          // 'Object Slicing' warning issued here
    useBase(baseobj);                        // ok: baseobj is a Base

    baseobj=derivednofieldsobj;  // 'Object Slicing' warning issued here only if OBJSLICE_WARN_NEW_MEMBER_ONLY=No
    useBase(baseobj);                        // ok: baseobj is a Base
  }
}

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。

 

To report problems with this documentation, please visit https://support.codesecure.com/.