Why this class doesn't make error? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
#include <iostream>
using namespace std;
Class ITEM{
private:
int cnt;
public:
ITEM(){}
void func(ITEM a){
a.cnt = 10;
}
};
int main(){
return 0;
}
I assume that red line will make the error.
because 'a.cnt' value is private value.
I learned that private value must be modified inner of class.

void func(A a){
a.cnt = 10; //valid
}
notice that function is inside the class, so it becomes it member and class member functions have access to private members.
Note that it is being modified inner of the class ITEM using a member function,which is perfectly valid

Related

How can I access member variables of a vector of objects that is a member variable from a class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I do know how to access member variables given a vector of objects but suppose
if I have a class called "layer" that is
class layer{
public:
layer(.... that initializes "val" .... );
vector<vector<double>> getval(){return val;}
private:
vector<vector<double>> val;
}
and then suppose there is another class that is
class Net{
public:
Net( ..... that initializes "nn" ..... );
vector<layer> getnn(){ return nn; }
private:
vector<layer> nn;
}
So in the main function, I could create an object like
Net n( ....... )
and in the main function I could get vector of objects via
n.getnn();
but the question is how could I get the specific, given i index,
vector<vector<double>> val
at nn[i]
float value = n.getnn()[i].getval()[j][k];

C++: struct accessing instance of itself [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have come across a rather unusual issue in my code. A struct needs to be able to access instances of itself.
Relavent portion of code:
struct crtr {
char f;
void foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
};
vector<crtr> creatures[10];
Of course, this is nowhere close to working - crtr.foo() requires creatures, while creatures requires crtr. Is there some way to initialize creatures before crtr, perhaps changing the vectors' data type? (preferably with minimal pointers, if possible)
I must be missing something, what's wrong with this?
struct crtr {
char f;
void foo();
};
vector<crtr> creatures[10];
void crtr::foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
Also crtr::foo can be inline if that's required.
Use forward declaration of the struct:
struct crtr;
vector<crtr> creatures[10];
struct crtr {
char f;
void foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
};

In C++, why can I call vector<double>::begin() in for a private content in a class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class AAA {
private:
vector<double> v;
public:
AAA(int n);
void AAAprint();
void testfn(AAA &aaa);
};
AAA::AAA(int n){
v=vector<double>(n,0);
}
void AAA::AAAprint(){
for (int i=0;(unsigned int) i<v.size();i++)
{
std::cout<<v[i]<<" ";
}
std::cout<<std::endl;
}
void AAA::testfn(AAA &aaa)
{
aaa.v[0]=5;
}
int main(){
AAA aaa(25);
aaa.AAAprint();
AAA bbb(25);
bbb.testfn(aaa);
aaa.AAAprint();
}
In main i first construct aaa then print out its value. Next aaa is changed after calling bbb.testfn(aaa). In void AAA::testfn(), I could directly access to aaa.v and the value of aaa.v[0] is indeed changed outside of void AAA::testfn(). Since I printout aaa.v in the end of main function it shows so. What is the reason for this?
Privcy is a class restriction not an instance restriction. All instances of a class have full access to private members of all other instances.
There are quirks about inheritance and protected and distinct instances, but that is another question.

Use of lambda during member variable initialization within class declaration [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
For readability, I like to initial member variables within class declaration. I am also intrigued by used of lambda during such initialization ( see snippet). My question is what are the advantages or disadvantages of using such lambda functions in the declaration over member functions.
class foo
{
private:
int index_ = 5;
int value_ = [](int index)
{
int result = 0;
for( int i = 0;i <index; i++)
result += i;
return result;
}(index_ );
};
Edited:
As bar::bar(int index) :index_(index) {} is more optimized than bar::bar(int index) {index_ = index;}, whether the above code performs better or it just the readability.

Why we cannot declare a function argument static? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have been curious about why c++ does not allow declaring a static function argument as shown below:
int test(static int a )
{
return a;
}
int main()
{
test(5);
return 0;
}
output console shows:
error: storage class specifiers invalid in parameter declarations
error: storage class specified for parameter 'a'
Update #1:
I can achieve my requirements like follows:
int test(int a )
{
static int count = 0;// <-- I want to eliminate this line due to some project constraints.
count += a;
return count;
}
I cannot use passing arguments by reference if you suggest, I have already tried considering that option.
If there is any other way to accomplish above behavior, you're welcome.
Thanks.
To declare a function static you would do it as such
static int test(int a )
{
return a;
}
You are trying to pass in a "static int a" into a function but there's no reason to do that. You would instead declare
static int a;
somewhere in the class and then simply pass in a to the static method created above as so
test(a);