Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Accessing private variables

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!!
I would say by a public member function which modifies the private variable.
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.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(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.
Well you could construct the first element to have whatever values you want...
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
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?
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
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
} 
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
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)
#


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by Z3R00NDATOct 16, 2023 01:24 PM 3 135 Oct 16, 2023 01:24 PM
Last Post: Disease
[C] by bhaktefhabo87Oct 11, 2021 07:28 AM 2 185 Oct 11, 2021 07:28 AM
Last Post: Cool_Pro_Grammer
[Python] by simmerMay 28, 2021 05:33 AM 5 202 May 28, 2021 05:33 AM
Last Post: simmer