How do I use two different class interfaces to the same underlying data?

V

velthuijsen

I've got a data class.
There is one point in the program I'm writing that needs access to the
non const functions of this class.
The rest of the program only needs the getters.
Currently I've solved this by handing out const pointers to this data
class to be used by the rest of the program.

But I don't like showing the non const functions to parts of the
program that just don't need them.
So I basically want two interfaces to the same underlying data.
One that allows reads and one that allows reads & writes.

Is this possible at all? If so where can I find an example/tutorial on
the net about it or in what book about C++ is this described?

P.s. while writing this I got an idea about a possible solution using
a base class with only the getters and a derived class that contains
the rest.
 
V

Victor Bazarov

velthuijsen said:
I've got a data class.
There is one point in the program I'm writing that needs access to the
non const functions of this class.
The rest of the program only needs the getters.
Currently I've solved this by handing out const pointers to this data
class to be used by the rest of the program.

But I don't like showing the non const functions to parts of the
program that just don't need them.
So I basically want two interfaces to the same underlying data.
One that allows reads and one that allows reads & writes.

Is this possible at all? If so where can I find an example/tutorial on
the net about it or in what book about C++ is this described?

I'd try "Advanced C++: Programming styles and idioms" by James Coplien.
P.s. while writing this I got an idea about a possible solution using
a base class with only the getters and a derived class that contains
the rest.

That's exactly what I was going to suggest.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top