Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding private data members? (C++)

Tags:

c++

private

class

Is there a way to hide private data members of a C++ class away from its users, in the cpp file? I think of the private members as part of the implementation and it seems a little backwards to declare them in the header file.

like image 781
Jeff Linahan Avatar asked Oct 15 '08 20:10

Jeff Linahan


2 Answers

The "pimpl" idiom is how this is generally handled.

See

  • http://www.gotw.ca/gotw/024.htm
  • http://www.gotw.ca/gotw/028.htm
  • http://herbsutter.com/gotw/_100/ (updated for C++11)
like image 67
2 revs, 2 users 92% Avatar answered Oct 11 '22 01:10

2 revs, 2 users 92%


you want to use something like the PIMPL idiom

http://en.wikipedia.org/wiki/Opaque_pointer

like image 21
Keith Nicholas Avatar answered Oct 11 '22 00:10

Keith Nicholas