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

11. Member Access Control

11.1 Access Specifiers


11.1.1 Declare all data members private

If direct access to the object state is allowed through public or protected member data, encapsulation is reduced making the code harder to maintain. By implementing a class interface with member functions only, precise control is achieved over modifications to object state as well as allowing for pre and post conditions to be checked when accessing data.

#include <string>
#include <cassert>
               
class C
{
public:
C ();
                 
std::string m_id; // @@- Non-Compliant [email protected]@
};

class D
{
public:
D ();
                 
std::string const & getId () const
{
assert (! m_id.empty () && "Id not yet specified");
return m_id;
}
                 
private:
std::string m_id; // @@+ Compliant [email protected]@
};

Exception

Class types declared with the class key struct in an extern “C” block are intended to be compatible with C, and therefore such definitions are not covered by this rule.

For example:

extern "C"
{
struct S // Compliant
{
int i;
int j;
};
}

References

  • HIC++ v3.3 – 3.4.1

View references >

11.2 Friends


11.2.1 Do not use friend declarations

Friend declarations reduce encapsulation, resulting in code that is harder to maintain.

class C
{
public:
C & operator += (C const & other);
                 
friend C const operator + (C const &, C const & lhs); // @@- Non-Compliant [email protected]@
};
               
class D
{
public:
D & operator += (D const & other);
};
               
D const operator + (D const & rhs, D const & lhs) // @@+ Compliant [email protected]@
{
D result (rhs);
result += (lhs);
return result;
}

The following example demonstrates an additional complexity when adding friendship for functions that include universal reference parameters. In such cases an explicit declaration is required for all combinations of the lvalue reference and rvalue reference versions.

template <typename T, typename S>
void foo (T && t, S && s)
{
t.foo ();
}
               
class A
{
private:
void foo ();
               
friend void foo(A &, A &);
friend void foo(A &, A&&);
friend void foo(A&&, A &);
friend void foo(A&&, A&&);
};
               
int main ()
{
A a;
foo(a, a);
foo(a, A());
foo(A(), a);
foo(A(), A());
}

Exception

In some cases friend declarations are part of the class interface, for example:

  • serialization via stream input and output operators (operator << and operator >>)
  • factory functions requiring access to private constructors
  • an iterator class

For example:

# include <cstdint>
class C
{
public :
C (C const &) = default ;
C (C &&) = default ;
private :
C ( int32_t );
friend C createClassC ( int32_t );
};
C createClassC ( int32_t i)
{
// pre condition check on ’i’
return C(i);
}

References

  • HIC++ v3.3 – 3.4.4

View references >

Request PDF Version

 

 

Book traversal links for 11. Member Access Control

  • ‹ 10. Derived Classes
  • High Integrity C++ Coding Standard
  • 12. Special Member Functions ›

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