Skip to main content

Secondary navigation

  • Downloads
  • Integrations
  • Blog
  • Company
    • About Us
    • Team
    • Culture
    • Careers
    • Partners
    • Press
    • Events
  • Contact
    • Contact Us
    • Request Support
    • Subscribe
  • Language
    • English
    • 日本語
    • 简体中文
    • 한국어
    • Deutsche
Home
Perforce

Main Navigation - Mega Menu

  • Products

    Main Navigation - Mega Menu

    • Explore Products
    • All Products

    Main Navigation - Mega Menu

    • Plan
    • Create & Develop
    • Test & Validate
    • Operate, Manage, & Scale
    Helix Core
    Helix4Git
    Helix TeamHub
    Helix ALM
    Hansoft
    Gliffy
    Perfecto
    TestCraft
    Helix QAC
    Klocwork
    Akana
    OpenLogic
    Zend Server
    TotalView
    XRebel
    HostAccess
    SourcePro
    Stingray
    HydraExpress
    PV-WAVE
    Visualization
    IMSL
    Methodics IPLM
    VersIC
    JRebel
  • Solutions

    Main Navigation - Mega Menu

    • Explore Solutions
    • Solutions Overview

    Main Navigation - Mega Menu

    • By Need
    • By Industry

    Main Navigation - Mega Menu

    • Plan
    • Create & Develop
    • Test & Validate
    • Operate, Manage, & Scale

    Main Navigation - Mega Menu

    • Aerospace & Defense
    • Automotive
    • Embedded Systems
    • Semiconductor
    • Energy
    • Financial
    • Game Development
    • Virtual Production
    • Government
    • Medical Devices
      Icon
    • Software

    Main Navigation - Mega Menu

    • Application Lifecycle Management
    • Agile Project Management
    • Diagramming

    Main Navigation - Mega Menu

    • DevOps
    • Version Control
    • IP Lifecycle Management
    • Java Application Development

    Main Navigation - Mega Menu

    • Web & Mobile App Testing
    • Codeless Selenium Automation
    • Static Analysis
    • Audit & Compliance

    Main Navigation - Mega Menu

    • API Management
    • Open Source Support
    • Enterprise PHP
    • HPC Debugging
    • Development Tools & Libraries
  • Customers
  • Resources

    Main Navigation - Mega Menu

    • Explore Resources
    • Papers & Videos
    • Recorded Webinars
    • Events
    • Blog
    • Demos
    • Subscribe
    Image VCS IPreuse Preview new

    How to Make IP Reuse Work For You

    Read Now
  • Support

    Main Navigation - Mega Menu

    • Explore Support
    • Support Plans
    • Self-Service Resources
    • Documentation
    • Video Tutorials
    • Training
    • Consulting
    • Release Notes
    • Download Software
    • Request Support

    Support

    Get answers quick by searching our public knowledgebase

    Visit Perforce KB

  • Try Free
  • Downloads
  • Integrations
  • Blog
  • Company

    Main Navigation - Mega Menu

    • About Us
    • Careers
    • Culture
    • Events
    • Partners
    • Press
    • Team
  • Contact

Version 4.0

High Integrity C++ Coding Standard

Released October 3, 2013

Request PDF Version
High Integrity C++ Coding Standard
0. Introduction
1. General
2. Lexical Conventions
3. Basic Concepts
4. Standard Conversions
5. Expressions
6. Statements
7. Declarations
8. Definitions
9. Classes
10. Derived Classes
11. Member Access Control
12. Special Member Functions
13. Overloading
14. Templates
15. Exception Handling
16. Preprocessing
17. Standard Library
18. Concurrency
19. References
20. Revision History
21. Conditions of Use

9. Classes

9.1 Member Functions


9.1.1 Declare static any member function that does not require this. Alternatively, declare const any member function that does not modify the externally visible state of the object

A non-virtual member function that does not access the this pointer can be declared static. Otherwise, a function that is virtual or does not modify the externally visible state of the object can be declared const. The C++ language permits that a const member function modifies the program state (e.g. modifies a global variable, or calls a function that does so). However, it is recommended that const member functions are logically const also, and do not cause any side effects. The mutable keyword can be used to declare member data that can be modified in a const function, however, this should only be used where the member data does not affect the externally visible state of the object.

#include <cstdint>
   
class C
{
public:
explicit C (int32_t i)
: m_i (i)
, m_c (0)
{
}
               
int32_t foo () // @@- Non-Compliant: should be static [email protected]@
{
C tmp (0);
return tmp.bar ();
}
                 
int32_t bar () // @@- Non-Compliant: should be const [email protected]@
{
++ m_c;
return m_i;
}
                 
private:
int32_t m_i;
mutable int32_t m_c;
};

References

  • HIC++ v3.3 – 3.1.8

View references >

9.1.2 Make default arguments the same or absent when overriding a virtual function

The C++ language standard allows that default arguments be different for different overrides of a virtual function. However, the compiler selects the argument value based on the static type of the object used in the function call. This can result in confusion where the default argument value used may be different to the expectation of the user.

#include <cstdint>
   
class Base
{
public:
virtual void goodvFn (int32_t a = 0);
virtual void badvFn (int32_t a = 0);
};
   
class Derived : public Base
{
public:
void goodvFn (int32_t a = 0) override; // @@+ Compliant [email protected]@
void badvFn (int32_t a = 10) override; // @@- Non-Compliant [email protected]@
};
   
void foo (Derived& obj)
{
Base& baseObj = obj;
   
baseObj.goodvFn ();  // calls Derived::goodvFn with a = 0
obj.goodvFn ();      // calls Derived::goodvFn with a = 0
   
baseObj.badvFn ();   // calls Derived::badvFn with a = 0
obj.badvFn ();       // calls Derived::badvFn with a = 10
}

References
  • Do not use default arguments – 8.3.3
  • HIC++ v3.3 – 3.3.12

View references >

9.1.3 Do not return non-const handles to class data from const member functions

A pointer or reference to non-const data returned from a const member function may allow the caller to modify the state of the object. This contradicts the intent of a const member function.

#include <cstdint>
   
class C
{
public:
C () : m_i (new int32_t) {}
                 
~C()
{
delete m_i;
}
               
int32_t * get () const
{
return m_i;  // @@- Non-Compliant [email protected]@
}
                 
private:
int32_t * m_i;
     
C (C const &) = delete;
C & operator = (C const &) & = delete;
};

Exception

Resource handler classes that do not maintain ownership of a resource are exempt from this rule, as in this context the rule conflicts with Rule 9.1.1: ”Declare static any member function that does not require this. Alternatively, declare const any member function that does not modify the externally visible state of the object”, which takes precedence.

For example:

# include <cstdint>
class D
{
public :
D ( int32_t * p) : m_i (p) {}
int32_t * get () const
{
return m_i; // Compliant
}
private :
int32_t * m_i;
};

References

  • HIC++ v3.3 – 3.4.2

View references >

9.1.4 Do not write member functions which return non-const handles to data less accessible than the member function

Member data that is returned by a non-const handle from a more accessible member function, implicitly has the access of the function and not the access it was declared with. This reduces encapsulation and increases coupling.

#include <cstdint>
   
class C
{
public:
C () : m_i (0) {}
                 
int32_t & get ()         // @@- Non-Compliant [email protected]@
{
return m_i;
}
   
int const & get () const // @@+ Compliant [email protected]@
{
return m_i;
}
   
private:
int32_t m_i;
};

Exception

Non-const operator [] is exempt from this rule, as in this context the rule conflicts with Rule 13.2.4: ”When overloading the subscript operator (operator[]) implement both const and non-const versions”, which takes precedence.

For Example:

# include <cstdint>
class Array
{
public :
Array () ;
int32_t & operator [] ( int32_t a) // Compliant: non-const version
{
return m_x[ a ];
}
int32_t operator [] ( int32_t a) const // Compliant: const version
{
return m_x[ a ];
}
private :
static const int32_t Max_Size = 10;
int32_t m_x [ Max_Size ];
};

References

  • HIC++ v3.3 – 3.4.3

View references >

9.1.5 Do not introduce virtual functions in a final class

Declaring a class as final explicitly documents that this is a leaf class as it cannot be used as a base class. Introducing a virtual function in such a class is therefore redundant as the function can never be overridden in a derived class.

class Base
{
public:
virtual void f1 ();   // @@+ Compliant [email protected]@
};
     
class Derived final : public Base
{
public:
virtual void f2 ();   // @@- Non-Compliant [email protected]@
};

9.2 Bit-fields


9.2.1 Declare bit-fields with an explicitly unsigned integral or enumeration type

To avoid reliance on implementation defined behavior, only declare bit-fields of an explicitly unsigned type (uintN_t) or an enumeration type with an enumeration base of explicitly unsigned type.

#include <cstdint>
enum E : uint8_t { ONE, TWO, THREE };
struct S
{
int32_t a : 2; // @@- Non-Compliant [email protected]@
uint8_t b : 2; // @@+ Compliant [email protected]@
bool    c : 1; // @@- Non-Compliant [email protected]@
char    d : 2; // @@- Non-Compliant [email protected]@
wchar_t e : 2; // @@- Non-Compliant [email protected]@
E       f : 2; // @@+ Compliant [email protected]@
};

References

  • JSF AV C++ Rev C – 154
  • MISRA C++:2008 – 9-6-2
  • MISRA C++:2008 – 9-6-3

View references >

Request PDF Version

 

 

Book traversal links for 9. Classes

  • ‹ 8. Definitions
  • High Integrity C++ Coding Standard
  • 10. Derived Classes ›

Footer menu

  • Products
    • Version Control System
    • Helix Core
    • Methodics IPLM
    • VersIC
    • Enterprise Agile Planning
    • Hansoft
    • Dev Collaboration
    • Helix TeamHub
    • Helix Swarm
    • Helix4Git
    • Development Lifecycle Management
    • Helix ALM
    • Surround SCM
    • Static Analysis
    • Helix QAC
    • Klocwork
    • Development Tools & Libraries
    • HostAccess
    • HydraExpress
    • PV-WAVE
    • SourcePro
    • Stingray
    • Visualization
  • Solutions
    • By need
    • Version Control
    • Application Lifecycle Management
    • Agile Project Management
    • Backlog Management
    • Project Portfolio Management
    • Audit & Compliance
    • DevOps
    • Static Analysis
    • IP Lifecycle Management
    • By industry
    • Game Development
    • Life Sciences
    • Software
    • Automotive
    • Embedded Systems
    • Government
    • Finance
    • Energy & Utilities
    • Aerospace & Defense
    • Virtual Production
    • Semiconductor
  • Resources
    • Papers & Videos
    • Events & Webinars
    • Recorded Webinars
    • Blog
  • Support
    • Documentation
    • Request Support
    • Video Tutorials
    • Training
    • Consulting
    • Release Notes
    • Download
  • Customers
    • Case Studies
  • About
    • Our Team
    • Our Culture
    • Careers
    • Press
    • Contact
  • Partners
    • Integrations
    • Resellers
  • Quick links
    • Try For Free
    • Helix Core Demo
    • Helix ALM Demo
    • Subscription Center
    • Customer Support Login
    • Licensing Requests
    • Educational Licenses
    • How to Buy
Home
Perforce

Copyright © 2021 Perforce Software, Inc. All rights reserved.  |  Sitemap  |  Terms of Use  |  Privacy Policy

Social menu

  • Facebook
  • Twitter
  • Google+
  • LinkedIn
  • YouTube
  • RSS