1

I am new to c++ programming. I have created a thread and a object of a class in it. This class is in another project. Now how to access the private members of this class in the thread because i want to set them.Their is no methods for setting this members. Can we anyone help me please. Thanks.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
varun
  • 43
  • 1
  • 6
  • "Project" is a bunch of source files; a "thread" is a run-time entity. It's not quite clear what you're trying to access and failing. Also, a concrete example (with some code) would be useful. Finally, only the implementation of class methods can access the class' private members - unless you use the [`friend`ing mechanism](http://en.cppreference.com/w/cpp/language/friend). – einpoklum Feb 12 '17 at 10:26

1 Answers1

1

Well, your question is somewhat vague, but if you want non-class-member code to be able to access private class member, you can add the function which needs to access the code as a friend of the class. This is highly unrecommended except in specific circumstances - since it breaks the encapsulation of your class and creates more complicated and delicate dependencies - and you should avoid doing so without a good reason.

Most likely, you need to reconsider why that member is private - and how that squares with your supposed need to access it from outside the class. Perhaps it just needs, say, a "getter" method which is guaranteed not to change the object (a const method)? Perhaps you can use another, public, method of the class instead of accessing the private member directly? Think about it.

Community
  • 1
  • 1
einpoklum
  • 118,144
  • 57
  • 340
  • 684