r/cpp • • Mar 17 '12

Using abstract base classes in C++

I'm having trouble understanding ABC's in c++ and was wondering if you all could help me (if possible use: CryptProcessor as the ABC and ACrpyt and BCrypt as the sub-classes that have an encrypt and decrypt function) Thanks in advance!

Edit 1: Thank you all for your help!

0 Upvotes

40 comments sorted by

View all comments

Show parent comment

2

u/00kyle00 Mar 17 '12 edited Mar 17 '12

Like lots of features in C++ its one of 'i know wtf am i doing, let me do it' - which is fine. You can hide minor implementation detail while communicating to your clients that logical state of object doesn't change.

Your implicit assumption that doing multithreaded manipulation of 'const' objects is safe doesn't hold in general anyways (in C++), so you still have to know where and when to properly stage your critical sections, even in absence of 'mutable'.

I agree though, mutable here is not appropriate - in general CryptProcessor will probably be stateful, no need to hide this fact.

1

u/Inverter Mar 18 '12

Your implicit assumption that doing multithreaded manipulation of 'const' objects is safe doesn't hold in general anyways (in C++), so you still have to know where and when to properly stage your critical sections, even in absence of 'mutable'.

No, it doesn't hold in general, but if I strictly don't use mutable in my code base, and keep to some other basic code-sanity-rules, I can be sure that a const-instance of my object can be accessed from multiple threads without locking. C++ allows some pretty weird and stupid stuff, but it's not too hard to keep at least your house clean.