Click here to Skip to main content
Licence CPOL
First Posted 18 Sep 2009
Views 18,156
Bookmarked 16 times

Hiding Implementation Details in C++

By | 19 Sep 2009 | Article
Get the implemention details out of the C++ include file

Introduction

I want to share a simple way of hiding implementation details in C++.

The Classic Way

A C++ class definition lets you control access to private data and methods while exposing a public interface to manipulate the data.

// Define a public interface in foo.h
class Foo
{
public:
   Foo() {}
   void SetPosition(float x, float y);
private:
   float m_x, m_y;    	// private data
   void PrivateMethod() {}	// private method
};
// Define a private implementation in foo.cpp
void Foo::SetPosition(float x, float y)
{
     m_x = x; m_y = y;
}

Problems...

While the code for the implementation is hidden in the CPP file, the encapsulation is incomplete because:

  1. private data members and methods are defined in the header file
  2. changing the implementation often requires changing the header file because
    • you add or change data members or
    • you add or change private methods

Ideally, the header file should be something you read to learn about the public interface for a class and nothing else.

Improved

To solve this problem, you can use inheritance.

// Define a public interface in foo.h
class Foo
{
public:
   virtual void SetPosition(float x, float y) = 0;
   static Foo* Create();
protected:
   Foo() {}    // hide
};
// Define a private implementation in foo.cpp
class FooImpl : public Foo
{
public:
   FooImpl() {}
   void SetPosition(float x, float y)
   {
        m_x = x; m_y = y;
   }
private:
   float m_x, m_y;   	// private data
   void PrivateMethod() {}	// private method
};

// Define factory method
Foo* Foo::Create()
{
    return new FooImpl();
}

The client no longer sees any of the implementation details. The cost is that we are now required to have a factory method and virtual methods.

Improved With No Virtual Methods

Adding virtual methods might not be acceptable if your class objects are streamed off disk (memory ready) or you don't want to incur the performance penalty. Here is a solution that works without virtual methods:

// Define a public interface in foo.h
class Foo
{
public:
   void SetPosition(float x, float y);
   static Foo* Create();
protected:
   Foo() {}    // hide
};
// Define a private implementation in foo.cpp
class FooImpl : public Foo
{
public:
   FooImpl() {}

   // allow Foo methods to see our private members
   float m_x, m_y;    	// private data
   void PrivateMethod() {}	// private method
};

// Define factory method
Foo* Foo::Create()
{
    return new FooImpl();
}

// downcasting methods
inline FooImpl * GetImpl(Foo* ptr) { return (FooImpl *)ptr; }
inline const FooImpl * GetImpl(const Foo* ptr) { return (const FooImpl *)ptr; }

// Define public method
void Foo::SetPosition(float x, float y)
{
    FooImpl * f = GetImpl(this);
    f->m_x = x; f->m_y = y;
}

By downcasting in the class methods, we get access to the implementation data without virtual methods.

Conclusion

This technique should probably not be used for small classes such as vectors, where hiding data members is not as important. In this situation, inline methods need access to the data in the header file. But any class that has a non-trivial data structure that might change would benefit from this way of insulating the header file from changes in the implementation.

History

  • Sep-2009: Article submitted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

milkplus

Software Developer
Buzz Monkey Software
United States United States

Member

David McClurg is a game programmer from Oregon, USA. He is currently interested in C#, xna for zune, and steering behaviors. When not coding, David enjoys tennis, kayaking, and botany.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberJoe Sonderegger21:53 16 Dec '09  
GeneralOne of the benefits you cite is non-existent (potentially worse) PinmemberLKarayan7:49 1 Oct '09  
GeneralMy vote of 2 Pinmemberbobyx829:08 25 Sep '09  
GeneralI liked it PinmemberGrump4:17 24 Sep '09  
GeneralSimple, but nice idea Pinmembercsantia6622:33 23 Sep '09  
GeneralGood idea to implement interface using Mix-in class, without virtual functions PinmemberTage Lejon3:38 22 Sep '09  
GeneralMemory leak! PinmemberVoropaev16:55 21 Sep '09  
GeneralRe: Memory leak! Pinmembermilkplus8:14 22 Sep '09  
GeneralRecommended book PinmemberVoropaev16:41 22 Sep '09  
GeneralQuestion is why PinmemberBob10004:35 21 Sep '09  
GeneralRe: Question is why PinmemberRealSkydiver9:52 22 Sep '09  
GeneralSorry, but not my solution PinmemberZaibot3:53 21 Sep '09  
GeneralRe: Sorry, but not my solution Pinmembermilkplus8:27 21 Sep '09  
GeneralRe: Sorry, but not my solution PinmemberZaibot1:35 22 Sep '09  
GeneralMy vote of 2 PinmemberKarstenK21:22 20 Sep '09  
GeneralRe: My vote of 2 PinmemberRick York10:06 22 Sep '09  
GeneralpImpl PinmemberPsiY22:48 19 Sep '09  
GeneralRe: pImpl Pinmembermilkplus18:01 20 Sep '09  
GeneralRe: pImpl PinmemberStefan6322:40 22 Sep '09  
Generalsuggests Pinmembercooolcode4:37 19 Sep '09  
GeneralRe: suggests Pinmembermilkplus5:56 19 Sep '09  
GeneralRe: suggests Pinmembercooolcode1:32 20 Sep '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 19 Sep 2009
Article Copyright 2009 by milkplus
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid