Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

accessing data of a class from a method of another class

biased36biased36 Posts: 9Member
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++?

Pls,reply.
Thanks in advance

Comments

  • LundinLundin Posts: 3,711Member
    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.