Jump to content

  • Log In with Google      Sign In   
  • Create Account

C++ encapsulation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Vanderry   Members   -  Reputation: 213

Like
0Likes
Like

Posted 25 December 2012 - 12:55 PM

Hi !

 

I've been philosophizing about where to draw the line when it comes to encapsulation. It seems to me that when a class needs to contain data types that would preferably be forward declared in the header file, it would be most beneficial to just forward declare one structure to hold all of its member variables.

 

The way I see it, this first of all reduces the number of dynamic allocations needed to just one. It reduces the amount of redundant information visible through the class interface and nearly eliminates the need to recompile any file that is dependent of the class.

 

Does this seem like sound design? I'm curious to know why it is not a more common practice.

 

- Dave



Sponsor:

#2 e‍dd   Members   -  Reputation: 2105

Like
4Likes
Like

Posted 25 December 2012 - 01:00 PM

It seems to me that when a class needs to contain data types that would preferably be forward declared in the header file, it would be most beneficial to just forward declare one structure to hold all of its member variables.

 

[...]

 

Does this seem like sound design? I'm curious to know why it is not a more common practice.

It's actually quite common. It even has a bunch of names: the "pimpl idiom", the "compiler firewall idiom", the "cheshire cat idiom" and the "bridge pattern".



#3 Álvaro   Crossbones+   -  Reputation: 13309

Like
3Likes
Like

Posted 25 December 2012 - 01:01 PM

Ninja'd by edd. But here's a link to Wikipedia.



#4 Vanderry   Members   -  Reputation: 213

Like
0Likes
Like

Posted 25 December 2012 - 01:12 PM

Ah, I never considered the similarity to that technique. Cool, then there's some established justice to complicate things a little :)



#5 Khatharr   Crossbones+   -  Reputation: 2998

Like
0Likes
Like

Posted 25 December 2012 - 02:37 PM

You may also want to peek at how that can relate to the flyweight pattern. :)
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#6 Ravyne   GDNet+   -  Reputation: 7322

Like
2Likes
Like

Posted 26 December 2012 - 04:48 AM

Keep in mind, however, that just because this pattern has some nice properties, it doesn't mean that this level of abstraction is necessarily appropriate or even desirable all the time. You need to consider how the class is to be used, its scope of responsibility, how stable its api is, and other things. For example, this wouldn't be appropriate for a 3D vector or matrix class, but might be appropriate for, say, a 3D renderer.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS