Hack Forums
[C] Accessing private variables - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] Accessing private variables (/showthread.php?tid=5229207)



Accessing private variables - ShannonL - Apr 6, 2016

Hey, I have a quick question.

I have a class called "Hashtag" with the private variables
  • string word

  • int startCount

  • More... But the rest don't matter for this question



In one of the functions, I declared a vector of type Hashtag...
vector<Hashtag> allTags;

My question is: If I want to set the "string word" variable to "Hello" in the first element of the vector, how do I access "string word" in order to do that?

Is it: allTags.at(i).word = "Hello";

Or am I wrong. Thank you!!


* - g4143 - Apr 6, 2016

I would say by a public member function which modifies the private variable.


* - 0xDEAD10CC - Apr 6, 2016

HashTagVector[0].SetString("Hello");

You'll create a setter function called SetString() which will deal with the private member variable. Naming in the example above is up to you, I chose names for clarity, and for no other reason.

Post your class code.


* - ShannonL - Apr 6, 2016

(Apr 6, 2016 08:11 PM)0xDEAD10CC Wrote:  HashTagVector[0].SetString("Hello");

You'll create a setter function called SetString() which will deal with the private member variable. Naming in the example above is up to you, I chose names for clarity, and for no other reason.

Post your class code.

The problem is, for this project, our teacher gave us the class definition. We aren't allowed to add functions. The only function that has to do with "string word" is "getWord", which only returns it, not sets it.


* - g4143 - Apr 6, 2016

Well you could construct the first element to have whatever values you want...


* - szuszi2 - Apr 7, 2016

add a constructor like this Hashtag(const char * s):name(s){}
and add a copy constructor, that way you can make a new hashtag and copy it


* - 0xDEAD10CC - Apr 7, 2016

Show us the class definition... If you can't change anything you understand you need to show us what you're working with for us to help, correct?


* - Goodies - Apr 8, 2016

You generally have "accessor" and "mutator" functions which interact with the private variable themselves, but prevent other objects from doing so directly.

PHP Code

<?php 
class Foo{
    
int x{0};
public:
    
Foo() = default;
    
Foo(int _x) : x{_x} {}
    
void setX(int newX) { x = newX;} // mutator
    
int getX(void) const { return x; } // accessor
} 



* - Shmebulock - Apr 9, 2016

You can not change the value of private variables when you are not "working in the class".

The only ways to change them are:

1. Creater methods which are changing the private vars
2. Declare your class as friend of another class. "Friends" are allowed to change also private variables in C++. (Google will give you basic examples)