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.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 days ago.
Improve this question
i am trying to initial a vector of a defined class in another instance, the test code is as:
#include <vector>
class A
{
public:
float x = 0.0;
};
class B
{
public:
std::vector<A> b;
};
int main()
{
B x;
for(int i = 0; i < 3; i++)
{
A y;
x.b.insert(x.b.begin(), y);
}
}
but is there a more brilliant writing for the initial of x?
something maybe like
x.b.insert(x.b.begin(), 3, A);
which i got error error: expected primary-expression before ‘)’ token
thx
edit: sorry, edited for a better example
No need for a loop or insert, just construct the vector and assign it
B.x = std::vector<A>(3);
or if you have a particular value in mind
A y = something;
B.x = std::vector<A>(3, y);
Maybe this code should be in the constructor for B.
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];
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]*/}
}
};
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
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);