Using the same double in different functions - c++

I have two functions which use the same doubles ie like
in the .h I declare
class MyClass : {
public :
double arg1, arg2;
void getVarA(double a, double b);
void getVarB(double a, double b);
void Reset();
}
and in my .C I have something like
void MyClass::Reset(){
arg1 = 0.0f;
arg2 = 0.0f;
}
void MyClass::getVarA(double a, double b){
arg1+=2*a;
arg2+=2*b;
return arg1-arg2;
}
void MyClass::getVarB(double a, double b){
arg1+=2+a;
arg2+=2+b;
return arg1-arg2;
}
The problem is that in principle I want to create a copy of arg1,arg2 each one taking values in each function (ie in each function to be possible to take different values) so that arg1,arg2 dont "speak" between the two different functions and whenever I change the arg1 in getVarA function not to commute with the arg1 in getVarB function.
Sorry, probably my example is poorly phrased but I am just newbie..
thanks

Make your variables arg1, arg2 as function local variables.
void MyClass:getVarA(double a, double b) {
double arg1, arg2;
arg1+=2*a;
arg2+=2*b
return arg1-arg2
}
void MyClass:getVarB(double a, double b) {
double arg1, arg2;
arg1+=2+a;
arg2+=2+b
return arg1-arg2
}
They will become different variables. None of changes made in getVarA arg1, arg2 will affect arg1, arg2 from getVarB
EDIT
According to #harper comment, it is strongly recommended to avoid uninitialized variables. You can't assume initial value of arg1 and arg2 variables. Your should explicit set initial value
double arg1 = 0.0;
double arg2 = 0.0;

"The problem is that in principle I want to create a copy of arg1,arg2"
You can do exactly that:
void MyClass::getVarA(double a, double b)
{
double arg1Copy = arg1;
double arg2Copy = arg2;
arg1Copy+=2*a;
arg2Copy+=2*b;
return arg1Copy-arg2Copy;
}
Now arg1 and arg2 are not modified by your method. In fact you can even declare your method as const to inform the compiler that you do not want the data members to actually be modified by your method.

This answer is based on the following interpretation of your question:
Each of the getVar functions is supposed to remember something about previous calls. The value returned from a function call is supposed to change based on what calls were made to that function in the past. For example, if you ran this code, x1 and x2 would have different values:
MyClass mc;
double x1 = mc.getVarA(1,1);
double x2 = mc.getVarA(1,1);
If you want x1 and x2 to have different values (because the first call is supposed to change what values are used in the second call), then this answer is appropriate. If you want x1 and x2 to have the same value (because you don't want getVarA to remember anything about previous function calls), nnesterov's suggestion to use local variables is a better answer than this one.
You want what is remembered about calls to getVarA to be independent from what is remembered about calls to getVarB.
With that in mind, here's the .h:
class MyClass {
public :
double _varA1, _varA2;
double _varB1, _varB2;
MyClass();
double getVarA(double a, double b);
double getVarB(double a, double b);
void Reset();
};
and here's the .c:
MyClass::MyClass(){
Reset();
}
void MyClass::Reset(){
_varA1 = 0.0;
_varA2 = 0.0;
_varB1 = 0.0;
_varB2 = 0.0;
}
double MyClass::getVarA(double a, double b){
_varA1 += 2*a;
_varA2 += 2*b;
return _varA1 - _varA2;
}
double MyClass::getVarB(double a, double b){
_varB1 += 2+a;
_varB2 += 2+b;
return _varB1 - _varB2;
}
Things to notice:
Each of the two get functions has its own set of instance variables. That way you can change one function's data without changing the other function's data.
The constructor calls Reset() so that the variables are initialized when you first create an instance of the class. That way you can predict what will happen the first time you call each get function.
The return type of the get functions has been changed to double, to match the implementation.

Related

How do I re-assign a function name in C++

I have a problem in C++ that is similar to this example problem. In this case I have two member-functions that have an identical interface. Based on the information in a string passed to the super function, I would like to assign one of the two member functions to the variable class_func. Is there a way to do this?
// test.hpp
class TestClass
{
public:
double master_function(double a, double b, std::string func_name);
private:
double add(double a, double b);
double subtract(double a, double b);
};
// test.cpp
double TestClass::master_function(double a, double b, std::string func_name)
{
if (func_name == std::string("Add") const auto& class_func = add;
else const auto& class_func = subtract;
return class_func(a, b);
}
// =========================================================================
double TestClass::add(double a, double b)
{
return a + b;
}
// =========================================================================
double TestClass::subtract(double a, double b)
{
return a - b;
}
In other words, I am trying to assign the member-function add or subtract to the name class_func, so the code underneath the if statement in master_function can be uniform regardless of which function the user wants to use. In the form shown below I get the error Reference to non-static member function must be called out, but I am not totally sure what this means or how to fix it. In addition, I am using a C++17 compiler, so if there is a modern approach that works best with C++17 I would be interested in learning it.
The term you are looking for is member function pointer, but we can do without explicitly specifying that type. The problem with your code is not only in the way you try to refer to a member function (that would be &TestClass::add), but also that you create those aliases in a nested scope (under if/else), meaning they won't be visible in the return statement.
The simplest change is this:
auto class_func = &TestClass::add; // pick one default
if (func_name == "Subtract")
{
class_func = &TestClass::subtract;
}
else
{
assert(func_name == "Add"); // optional
}
return class_func(a, b);
This works because the add and subtract functions have the exact same type:
double (TestClass::*)(double a, double b)
But yeah, why are those functions not static? They do not work with a class' instance. Make them static and the above will still work, just note that the type of class_fun will be a simple function pointer:
double (*)(double a, double b)
Now that you know the types, you could change this in a way that does not privilege one function before the other in the code:
decltype(&TestClass::add) class_func = nullptr;
if (func_name == "Add")
{
class_func = &TestClass::add;
}
else if (func_name == "Subtract")
{
class_func = &TestClass::subtract;
}
assert(class_func != nullptr);
return class_func(a, b);
As mentioned in the comments, as that if-else chain starts to get longer, it makes more and more sense to use a (hash)map between strings and function pointers.

How to access a variable from a void return function in C++

I am new to C++ and I am contributing to a rather large project. I have written a piece of code and I am calling an external function that carries out a bunch of calculations.
I need external_function() to run through completely, but I also need the value of a specific variable (a double) that is calculated during external_function(). I would like to store or at least use the value of this variable in my_function(). Is this possible? Something along the lines of
double my_variable = external_function(external_variable);
Note the code looks like this:
void external_function()
{
double d;
// perform calculations on d
}
void my_function()
{
...
external_function();
...
}
Unfortunately, external_function does not return anything, it just does calculations and prints some output. As the entire code of the project is already rather complex, I would like to change as little as possible in the part of the code that has not been written by me. I would really appreciate your help!
I'm assuming here you have code like the following:
void external_function()
{
double d;
// perform calculations on d
...
// print d
std::cout << d;
}
void my_function()
{
...
external_function();
...
}
I'll assume external_function takes no parameters, but it really doesn't matter if it does.
You can change external_function by modifying the return type to double:
double external_function()
{
double d;
// perform calculations on d
...
// print d
std::cout << d;
return d;
}
The function can still be called safely like so:
external_function();
Without catching the return value, so there is no need to update other uses of it. Some static code analysers might hassle you about ignoring the return value of a function, but you can probably write an exception for them if you want.
This now means you can call it like so:
double value = external_function();
Your second option would be to pass an optional parameter to external_function():
void external_function(double* out = nullptr)
{
double d;
// perform calculations on d
...
// print d
std::cout << d;
if (out)
*out = d;
}
The same thing goes: callers can still call this function without changing the way they call it:
external_function();
But it means you can now call it like this:
double value = 0.0;
external_function(&value);
// value now contains the same value as d
If the function external_function returns a double, then yes, you can store that in a double as you have shown in the question. That will be perfectly fine.
If the double variable you are talking about is a local variable in that function that is not returned or stored in a variable passed to the function by reference, then you do not have any way of fetching that.
Yes, it's possible to store the value of external_function(external_variable) in a variable.
Be sure to check that the return type of external_function is double and, therefore, returns a double value. You will need to code it like this :
double external_function() {
double returnedValue;
// your code here
cout << calculationOutputValue << endl;
return returnedValue;
}
Mode 1 : Use void external_function(), adding parameter.
void external_function(double* pValue)
{
*pValue = external_value;
// operation of external_value
// external_value = 142.14
}
Get result
void call_function()
{
double pValue = 0.0;
external_function(&pValue);
cout<<pValue<<endl;
}
Result : 142.14
Mode 2 : Use no parameter function, modifying return type of function.
double external_function()
{
// do something you want...
// external_value = 142.14;
return external_value;
}
Get result
void call_function()
{
double pValue;
pValue = external_function();
cout<<pValue<<endl;
}
Result : 142.14
You can use a class member variable of double type.
Assign the calculated value of "d" to the member variable.
Now you can use this member variable in any method of your class.
or
You can pass a reference parameter from your calling method and assign the value of "d" in the externalfunction.
Eg:
externalFunction(double &updatedValue)
{
//do calculation of d
updatedValue =d;
}
void my_function()
{
double value;
externalFuntcion(value);
//now value will have the value of d;
}

C++ pass a function as argument

I need to call a library function in C++, it has the form:
double brents_fun(std::function <double(double)> f);
I can call this function by the following:
double fun1(double x)
{
return -2.0*x+1;
}
void main()
{
brents_fun(fun1);
return 0;
}
I want to pass another function to brents_fun, like:
double fun2(double x, double y)
{
return -2.0*x+y;
}
void main()
{
y=12.0;
brents_fun( fun2(x,y=12) );
return 0;
}
In my real code, y is complicated. I need to read data from file, do some calculations with the data to generate y. That's why I need two argument x and y in fun2. y will not be changed during calling brents_fun.
Since y is not changed, is there a way to pass fun2 to brents_fun?
Thank you.
Hao
You basically want to partially apply a function to pass it as an argument to brents_fun, this is possible and you have two ways to do it in C++11:
with a lambda
with std::bind
Both solutions:
double brents_fun(std::function <double(double)> f)
{
return f(0.0);
}
double fun1(double x, double y)
{
return x+y;
}
int main() {
double res1 = brents_fun([](double x) { return fun1(x, 12.0); });
double res2 = brents_fun(std::bind(fun1, placeholders::_1, 12.0));
}
While the lambda one is more suitable to optimizations in general both solutions are equivalent. std::bind can be polymorphic while a lambda can't, but that's not the point in your situation. I'd say just stick to the syntax you prefer.
brents_fun accepts as argument a function in the form of: double someFunction(double), as you might already know. No functions of other return types, nor arguments, can be used. In this particular case, you can pass the fun1 function, them sum y when you have it calculated. Also, to post formatted code, simply select it and ctrl+k.

How to guard against function arguments being passed in the wrong order?

Say I have a C++ function that looks like this:
double myfunction(double a, double b) {
// do something
}
Which I then call like this:
double a = 1.0;
double b = 2.0;
double good_r = myfunction(a, b);
double bad_r = myfunction(b, a); // compiles fine
I would like to make sure that a and b are never provided in the wrong order.
What is the best way to ensure this in C++?
Other languages allow named parameters, like this:
double good_r = myfunction(a=a, b=b);
double bad_r = myfunction(a=b, b=a); // mistake immediately obvious
double bad_r = myfunction(b=b, a=a); // compiles fine
Or perhaps the problem can be partly solved using types, i.e.
double my_type_safe_function(a_type a, b_type b) {
// do something
}
a_type a = 1.0;
b_type b = 2.0;
double good_r = myfunction(a, b);
double bad_r = myfunction(b, a); // compilation error
EDIT: A couple of people have asked what I mean by the "wrong order." What I mean is that, in real code a and b have some significance. For example, the arguments might instead be height and width. The difference between them is very important for the function to return the correct result. However, they are both floats and they both have the same dimensions (i.e. a length). Also, there is no "obvious" order for them. The person writing the function declaration may assume (width, height) and the person using the function may assume (height, width). I would like a way to ensure this doesn't happen by mistake. With two parameters it is easy to be careful with the order, but in a large project and with up to 6 arguments mistakes creep in.
Ideally I would like the checks to be done at compile time, and for there to be no performance hit (i.e. at the end of the day they are treated as plain old floats or whatever).
How about this:
struct typeAB {float a; float b; };
double myfunction(typeAB p) {
// do something
return p.a - p.b;
}
int main()
{
typeAB param;
param.a = 1.0;
param.b = 2.0;
float result = myfunction(param);
return 0;
}
Of course, you can still mess up when you assign your parameter(s) but that risk is hard to avoid :)
A variant is to have one struct per "new" type, and then make them go away in optimized builds using macros.
Something along these lines (only slightly tested, so it could be way off):
#define SAFE 0
#if SAFE
#define NEWTYPE(name, type) \
struct name { \
type x; \
explicit name(type x_) : x(x_) {}\
operator type() const { return x; }\
}
#else
#define NEWTYPE(name, type) typedef type name
#endif
NEWTYPE(Width, double);
NEWTYPE(Height, double);
double area(Width w, Height h)
{
return w * h;
}
int main()
{
cout << area(Width(10), Height(20)) << endl;
// This line says 'Could not convert from Height to Width' in g++ if SAFE is on.
cout << area(Height(10), Width(20)) << endl;
}
I think you already provided the easiest solution, using types.
One alternative could be using a builder class and method chaining.
Like:
class MyfunctionBuilder {
MyFunctionBuilder & paramA(double value);
MyFunctionBuilder & paramB(double value);
double execute();
(...)
}
Which you would use like this:
double good_r = MyFunctionBuilder().paramA(a).paramB(b).execute();
But this is a lot of extra code to write!
What is the "wrong order" actually? In this example of yours
double myfunction(double a, double b) {
// do something
}
double a = 1.0;
double b = 2.0;
double good_r = myfunction(a, b);
double bad_r = myfunction(b, a);
how do you actually want to know if this is the right order? What if the variables would be named "quapr" and "moo" instead of "a" and "b"? Then it would be impossible to guess whether the order is right or wrong just by looking at them.
With this in mind, you can do at least two things. First, is to give meaningfull names to the arguments, e.g.
float getTax( float price, float taxPercentage )
instead of
float getTax( float a, float b )
Second, do the necessary checks inside:
float divide( float dividend, float divisor )
{
if( divisor == 0 )
{
throw "omg!";
}
}
It is possible to do more complex checks, such as making a functor, and setting it's parameters explicitly, but in most of the cases that just complicates things without much benefit.

Passing a member function pointer(s)

My situation is following, I have two different bisection functions what will be called at some point in my code. Basically some function calls Bisection2 and this function calls either the passed function or it passes the function pointer to Bisection function.
in header I have
std::vector<double> F();
double F1(double m1, double m2);
double F2(double m1, double m2);
typedef double (MyClass::*MyClassFn)(double,double);
double Bisection(MyClassFn fEval,double min, double max,std::vector<double> args);
bool Bisection2(MyClassFn fEval1,MyClassFn fEval2,double xmin, double xmax, double ymin, double ymax,double *ax, double *ay,std::vector<double> args);
And my bisection functions look like this. I didn't include all the code because it's not necessary.
double MyClass::F1(double m1, double m2) {
m_m1 = m1;
m_m2 = m2;
F();
return m_my;
}
double MyClass::F2(double m1, double m2) {
m_m1 = m1;
m_m2 = m2;
F();
return m_mx;
}
double MyClass::Bisection(MyClass fEval,double min, double max,std::vector<double> args)
{
// Setting a lot of stuff here, including auxiliary and leftvalue...
MyClass *pObj = new MyClass(-1);
leftvalue = pObj->*fEval(auxiliary, left);
ightvalue = pObj->*fEval(auxiliary, right);
// Comparing and setting values here etc.
}
bool MyClass::Bisection2(MyClassFn fEval1,MyClassFn fEval2,double xmin, double xmax, double ymin, double ymax,double *ax, double *ay,std::vector<double> args)
{
// Setting some values here but these have nothing to do with the problem.
double yl;
double leftvalue, rightvalue, middlevalue;
MyClass *pObj = new MyClass(-1);
// Setting some values here but these have nothing to do with the problem.
std::vector <double> arg;
// pushing some values
yl = Bisection(fEval2,ymin,ymax,arg); // Here is the first way how I need to pass fEval2 to Bisection function.
arg.clear();
if(isnan(yl))
{
return M_NAN;
}
leftvalue = pObj->fEval1(xl, yl); // And here is the second way how I need to use fEval1.
//.....
}
And then I have basically a function what calls
`Bisection2(F1,F2, m_m2,0.0, 0.0, m_max2, &m_mu1, &m_mu2,args);
The Bisection2(...) call may be incorrect at the moment because I've changed the functions a lot since this worked last time. Last time I basically called F1 and F2 function pointers directly inside the functions instead of fEval's but I'm quite sure it was incorrect way after all even thought it seemed to work somehow.
Now leftvalue = pObj->*fEval(auxiliary, left); causes compiling errors:
error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘fEval (...)’, e.g. ‘(... ->* fEval) (...)’
I've tried to see help from here http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2
and also checked maybe different solved problems in these forums but still can't figure out what I'm doing wrong.
Thank you.
As the error message says, you need parentheses. This is because the function call has higher precedence than the ->* operator:
leftvalue = (pObj->*fEval)(auxilary, left);
^ ^
Also, you almost certainly shouldn't be using new here; you can fix the memory leaks using automatic storage:
MyClass obj(-1);
leftvalue = (obj.*fEval)(auxiliary, left);
This is simply a matter of priority :
Instead of doing pObj->*fEval(aux, left), just do (pObj->*fEval)(aux, left)