| ceruleus (215) | |||||||
|
Hello all, I've got a beginner question about object orientation in c++... Let's say I have two interfaces, foo.h and bar.h
And let's say I have one class implementing both of these interfaces
Okay, so here's what I want to do. I want the variable m_dwBarThings to ONLY be accessible by methods that were inherited from bar.h. So therefore it is in scope for DoThings( ) but DoStuff( ) does not have access to it, neither do any other non-interface methods like CalculateSomeStuff( )I know I can just write the code so those methods don't access that member variable lol. I want to prevent future developers from accidentally using that variable somewhere else in the class. Let's also say that this class can be used/accessed by multiple threads (if that matters to you guys). Here's my best guess, you guys will suggest that I put m_dwBarThings into bar.h as a protected member variable, then FooBarImpl gets it for free.
| |||||||
|
|
|||||||
| Peter87 (3917) | |||
You could create two classes. One that inherits foo and another that inherits bar, and let FooBarImpl inherit from both these classes.
| |||
|
|
|||
| ceruleus (215) | |
|
@Peter87 - Everyone loves layers! (just kiddig) I think this is the approach I will take. Thanks. | |
|
|
|
| Disch (8623) | |||
This seemed a little more obvious to me:
| |||
|
|
|||
| ceruleus (215) | |
|
@Disch- That was my initial thought too. Unfortunately I have guidelines I need to follow and my interfaces are all abstract, pure virtual with no evidence as to what goes on in the implementation. Just pure virtual functions and documentation in the interfaces. Thanks for the input though. Do you think that's a much better solution guidelines aside?? Thanks | |
|
|
|
| Disch (8623) | |
|
No, not really. Though I don't really see why you'd have the need for the original problem =x. Whether or not you need the variable depends on the implementation, so that variable should be defined in whatever class provides the implementation. | |
|
|
|