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)) {
Related
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
The problem is to implement class Cplx with two doubles x and y represent real and imaginary part of complex numbers.
One of the subtask is to implement operator -> with following description:
(z->re and z->im): access to the real and imaginary part of z(You must implement changing like z->re = 5).
I have troubles with operator -> I never really understand how it works so my question is: how -> works and when to use it and how to apply that idea in this problem.
The following does what you ask... But not sure it is what you want:
template <typename T>
struct ReIm
{
const ReIm* operator ->() const { return this; }
ReIm* operator ->() { return this; }
T re;
T im;
};
struct Cplx
{
double x;
double y;
ReIm<double> operator ->() const { return {x, y}; }
ReIm<double&> operator ->() { return {x, y}; }
};
Demo
The -> operator works to dereference a pointer to an object and get a member variable/function in one operator. For example,
Cplx* cplxPointer = new Cplx();
cplxPointer->x = 5;
Is the same as
Cplx* cplxPointer = new Cplx();
(*cplxPointer).x = 5;
It just dereferences the pointer and then gets the member variable (or function if you want). Unless I misunderstood your question, the above should be able to help you complete your assignment.
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 5 years ago.
Improve this question
My class has an array of objects, call it Foo. It is stored as Foo* m_Foos in the class. Say it has a value at [0], guaranteed, and Foo has a property called IsSetthat's just a bool or something.
void TryThis()
{
Foo returnValue;
GetValue(returnValue);
returnValue.IsSet = true;
if(m_Foo[0].IsSet != returnValue.IsSet)
{
// ERROR!!!!
}
}
void GetValue(Foo &container)
{
container = m_Foos[0];
}
Can anyone explain why m_Foo[0] =/= returnValue? Where is the error in my syntax?
I expect m_Foo[0] to be the same reference as returnValue, the same Foo in memory.
TryThis() is not modifying the Foo object that is stored in the m_Foos array.
GetValue() is assigning the Foo object from m_Foos[0] to another Foo object that is local to TryThis(). A copy is being made during that assigment. TryThis() is then modifying the copy, not the original.
If you want TryThis() to modify the original Foo object directly, you need to do something more like this instead:
void TryThis()
{
Foo &returnValue = GetValue();
returnValue.IsSet = true;
// m_Foo[0] is set true.
}
Foo& GetValue()
{
return m_Foos[0];
}
Or this:
void TryThis()
{
Foo *returnValue;
GetValue(returnValue);
returnValue->IsSet = true;
// m_Foo[0] is set true.
}
void GetValue(Foo* &container)
{
container = &m_Foos[0];
}
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);
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
This is a sample program for my problem, I am using VisualStudio 2008
void abc()
{
static int i = 0;
if (i==0)
{
xyz();
i++;
}
abc();
}
The static variable retain the value one in next debug session also, thus not calling xyz(), how can I call a function just once without using static variable??
How about this:
void abc(int init)
{
if(init == 1) xyz();
abc(0);
}
int main(void) {
abc(1);
}
It has the advantage of showing clearly what is going on. You could even declare an enum:
enum INIT {FIRST_TIME, RECURSING};
and do
void abc(enum INIT init) {
if(init == FIRST_TIME) xyz();
abc(RECURSING):
}
You can see a complete example at work at http://codepad.org/7euiC5LQ
#include <stdio.h>
enum INIT {FIRST, RECURSING};
void abc(enum INIT init) {
if(init == FIRST) {
printf("first time\n");
abc(RECURSING);
}
else {
printf("last time\n");
}
}
int main(void) {
abc(FIRST);
}
In this example, the second time is the last time. Obviously you can embellish from there; usually you will want to pass a parameter to your abc function that might decrease with each call until you reach some point that says "this is the end of the recursion" (think factorials, Fibonacci, etc). In that case, passing an "invalid" parameter (e.g. -1) for the initial call would be a good solution. You still have only one parameter.
Finally - when you are using C++, you could consider overloading your function. Call it with a parameter, and you include xyz; call it without, and you don't. A bit like the abcStart of one of the other answers. But since you tagged your question both C and C++, and there was no evidence in your code that you really intended C++, I am not even going there...
You can pass a callflag to abc() function as an indication that whether to call xyz() function or not.
void abc(int callflag){
// do somwork
if(callflag)
xyz(); // xyz() willbe called when callflag = !0
// do other stuff
abc(0)
}
void abcStart(){
abc(1);
//abc(0); If you don't want to call xzy even for first time.
}
I think this is flexible call xyz() within abc() whenever you wants.
Not sure this is what you're looking for but it works
void abc(){
abc();
}
void abcStart(){
xyz();
abc();
}
int main(){
abcStart();
}
Doing this you don't need to specify any flag or use any if. You just call the "start" function of your recursion
Use a Boolean variable in the caller and pass it to the called function.
#include <stdbool.h> // C99 and latter supports.
void abc(bool flag)
{
if (flag)
{
xyz();
flag = false;
}
abc(flag);
}
int main(void) // T is return type
{
...
bool flag = true;
abc(flag);
...
}
Here's a solution which doesn't hard code it to running xyz a single time i.e. you could later trivially change it to run it an arbitrary number of times (credit to Floris whose answer I adapted):
void abc(int i)
{
if(i > 0)
{
xyz();
i--;
}
abc(i);
}
int main()
{
abc(1);
}
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