0

Alright, I'm coding something for fun, but here's the problem:

I have a class with a private string variable that I want to change, but I want to change it to a line I read from a text file. Is there no other way than to create a separate string to get the text file and then use a member function to change the private variable?

this is my first question here, so tell me if I need to clarify things

  • 1
    What would you like to be able to do with the class's private string variable? – DavidO Dec 23 '13 at 07:02
  • You can use the variable as the destination for the `fread` function that reads from the file – Noam Rathaus Dec 23 '13 at 07:03
  • 1
    private variables can be read and write using member functions so if you dont want to create a new variable, u have to write a class level function to read the file and store a required value in the private variable – Usman Waheed Dec 23 '13 at 07:03

2 Answers2

1

Private members are designed to be manipulated using public methods of the class. If you don’t need to do anything sophisticated with them, it is OK to create getters and setters – methods just to read or write private variables (might also check the value or so).

#include <string>

class A
{
private:
    std::string privateString;

public:
    std::string getPrivateString() const { return privateString; }
    void setPrivateString(const std::string& newString) { privateString = newString; }
};

If your class should work with files, you could create a public method to read the line from file and store it to private member directly.

Melebius
  • 6,183
  • 4
  • 39
  • 52
  • 1
    Note that @Melebius has used pass-by-value semantics for his getter and setter methods. Depending on your compiler, this can give a handsome performance boost (see [Want Speed? Pass by Value.](http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/)). Depending on the usage of class A, you may choose to use pass-by-reference instead, in which case you should place const qualifiers appropriately to avoid having users of your class modify contents inadvertently, e.g. `const std::string & getPrivateString() const` and `void setPrivateString(const std::string & newString)`. – Rob Dec 23 '13 at 07:36
  • If I could nitpic, would you mind placing a const after the definition of getPrivateString()? Thanks. – Rob Dec 23 '13 at 07:38
  • @Rob OK, I added references and `const` qualifiers to my example although my original intention was to write minimal yet working code. However, I still kept `getPrivateString()` method returning value because you can work with it with less restrictions. There could even be both getters together. – Melebius Dec 23 '13 at 08:08
  • @Melbius Thanks! Just wanted the one `const`, and not for you to also add a pass-by-reference example as well. Entirely agree that minimal code example is better :) – Rob Dec 23 '13 at 08:11
  • I understand the using public methods to manipulate private members, but how would I use the method with reading the text file? – user3128724 Dec 24 '13 at 23:32
  • @user3128724 That’s another question, and a bit vague question. See http://www.cplusplus.com/doc/tutorial/files/ or http://stackoverflow.com/questions/7868936/c-read-file-line-by-line, for example. – Melebius Dec 26 '13 at 08:09
0

How about changing that variable in any member function?

Member function will have privilege to edit its private member variables.

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31