Issue of the default parameter in class [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 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.

Related

better cpp vector insert(initial) writing [closed]

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.

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)) {

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 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

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);