accessing data of a class from a method of another class

Hellow everybody.....
I am facing a problem in C++......not facing the same problem when I use C.
In my project there is a class VoiceChannel....there is a variable
char ani[255];
In a class EventHandler, an object of VoiceChannel,say, VC has been created and by calling some built-in functions VC->ani has been set some value.
Now,what I want is to use that value(VC->ani) from a method of InboundChannel Class which is a subclass of VoiceChannel.

If I use C then it is possible to store that value in a stucture
and use it as needed.But what can I do in the context of C++?

Thanks in advance


  • C++ is intentionally designed in this way to prevent from poor programming practice. It is using the keyword "private" for class members, in order to "encapsulate" them. This is the most fundamental concept of object-orientation: to create autonomous objects that don't care about or depend of the rest of the program. And in return, the rest of the program shouldn't care about how a particular class is implemented.

    What you need to do is to get the private variable is to write a member function that gives access to it. How to do that depends on the nature of the data. What you need to ask yourself is "why do I need access to this variable?". In object-oriented design, all code that handles a certain variable should be stored in the same class as said variable.

    This works in the very same way in C. There you use static variables in the global scope instead: less elegant but with the same purpose, encapsulation.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion