Why we cannot declare a function argument static? [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
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);

Related

How avoid error on not evaluated code C++? [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 2 years ago.
Improve this question
Apologies for the most ambiguous and bizarre title.
Suppose we have 2 classes A and B.
class B has interface hasSmth but class A has not.
How to make this code evaluate without compile errors?
class A {
//..implementation
int id() { return 1; }
};
class B {
//..implementation
int id() { return 2; }
bool hasSmth() { return true; }
};
int main()
{
auto obj = someFunction();//returns A or B
if (obj.id() == 1 || (obj.id() == 2 && obj.hasSmth())) {
...
}
}
If the function returns obj of type B, then we are good.
But if it returns obj of type A, compiler will complain about A not having hasSmth, regardless of that part of if never been evaluated.
Can someone give a workaround please?
Can someone give a workaround please?
Read the declaration of someFunction to see what it returns. In the case it doesn't return B, then don't write obj.hasSmth(). Problem solved.
Now, let's change the question a bit. Let's say that you want to make this work without knowing the return type. Perhaps because rather than main you may be actually writing a template that works with different types. There are several approaches, but function overloads are a simple one:
bool check([[maybe_unused]] const A&) {
return true;
}
bool check(const B& b) {
return b.hasSmth();
}
template<bool returnsA>
void foo() {
auto obj = someTemplate<returnsA>(); // returns A or B
if (check(obj)) {

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]*/}
}
};

Why this class doesn't make error? [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
#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

Issue of the default parameter in 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 9 years ago.
Improve this question
I have a question about default parameter in C++ class:
#include <stdio.h>
class Test {
public :
Test(int ai = 0, int bi = 0)
: a(ai), b(bi) { }
void print() { printf("a = %d, b = %d", a, b); };
private :
int a, b;
};
int main() {
Test t(1);
t.print();
return 0;
}
In this code,
result will be "a = 1 , b = 0(default parameter)"
If I want to set "a" is default parameter and "b" is other value, how should I write the code?
Is there any way to solve this question?
In C++, you may omit function arguments that are declared with default parameters, but you can only omit one or more function arguments from the end of the argument list. So, given
Test(int ai = 0, int bi = 0)
there is no way to pass a value for bi and leave ai as the default value.

Ambiguous FunctionOverloading in C++? [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 8 years ago.
Improve this question
I have a class A and it has two method foo which are actually overloaded. The class somewhat look like this
class A
{
public:
bool foo(int& a);
bool foo(size_t& a);
};
bool A::foo(int& a)
{
return true;
}
bool A::foo(size_t& a)
{
int new_a = a;
return foo(new_a); // here Cl throws me warning C4717: 'hweudm::UDMAbstractBaseEntity::SetAttribute' : recursive on all control paths, function will cause runtime stack overflow
}
int main()
{
A aObj;
size_t val = 12;
aObj.foo(val);
return 0;
}
From the code it does not look ambiguous. But I don't want to this warning during compilation. So can Anyone tell me
Why I am getting this warning even though I have type casted size_t to int ?
whether this will be be an error not a warning on GCC.
The code isn't correct. The result of the conversion-cast (int)a is an rvalue, and it cannot bind to either lvalue reference.
The only thing that would come close, if it weren't horribly undefined behaviour, would be something like foo(reinterpret_cast<int &>(a)), but don't do this, since it is not correct.
isn't size_t:
typedef unsigned int size_t;
? then A::foo(int& a) and A::foo(size_t& a) are exactly the same thing