F
firegun9
Hello everyone,
here is my program:
///////////////////////////////
#include <iostream>
using namespace std;
void multi(double* arrayPtr, int len){
for(int i=0; i<len; i++)
*(arrayPtr+i)*=2;
}
typedef void (*p2f) (double* a, int b);
//////////////////////////////
template <class T>
class bf{
private:
int type;
T var;
public:
bf(int i, T value){ type=i; var=value;}
void eval(double* dPtr, int len);
};
template <class T>
void bf<T>::eval(double* dPtr, int len){
switch(type){
case 0:
for(int i=0; i<len; i++)
*(dPtr+i)=var;
break;
case 1:
for(int i=0; i<len; i++)
*(dPtr+i)=*(var+i);
break;
case 2:
var(dPtr, len);
break;
}
}
void main(){
double a=0.0;
bf<double> a1(0,a);
double b[3]={0.0, 1.0, 2.0};
bf<double*> a2(1,b);
bf<p2f> a3(2,multi);
double temp[3]={5.0, 5.0, 5.0};
a1.eval(temp,3);
for(int i=0;i<3;i++)
cout<<temp<<endl;
}
I have a bf class with a member variable "var" whose type is defined as
a template. In bf's constructor, I use (int type) to record what type
var is defined.
The main purpose of bf is to retrieve a double array, then modified it
using the var member.
By different types of var, it does different modifies to the input
array.
There are 3 conditions:
If var is a double( it can be known from "type" variable), it fill the
incoming array with the double value.
If var is a double array, it copies its value one by one into the
incoming array.
If var is a function pointer, it takes the incoming array as the
argument.
There's no problem untill I call bf::eval() in main();
It seems like that if var is a double then the code "var(dPtr, len);"
is wrong. In fact, it is wrong. But that is in case 2. Whenever the var
is not a function pointer, it will never go into case 2 in the switch
in eval();
The familiar situation also happens when the var is set to a function
pointer. The error was found in case 0, in which var is treated as a
double.
I just wanna control the branch call by myself.
Is there any solution?
Thanks
here is my program:
///////////////////////////////
#include <iostream>
using namespace std;
void multi(double* arrayPtr, int len){
for(int i=0; i<len; i++)
*(arrayPtr+i)*=2;
}
typedef void (*p2f) (double* a, int b);
//////////////////////////////
template <class T>
class bf{
private:
int type;
T var;
public:
bf(int i, T value){ type=i; var=value;}
void eval(double* dPtr, int len);
};
template <class T>
void bf<T>::eval(double* dPtr, int len){
switch(type){
case 0:
for(int i=0; i<len; i++)
*(dPtr+i)=var;
break;
case 1:
for(int i=0; i<len; i++)
*(dPtr+i)=*(var+i);
break;
case 2:
var(dPtr, len);
break;
}
}
void main(){
double a=0.0;
bf<double> a1(0,a);
double b[3]={0.0, 1.0, 2.0};
bf<double*> a2(1,b);
bf<p2f> a3(2,multi);
double temp[3]={5.0, 5.0, 5.0};
a1.eval(temp,3);
for(int i=0;i<3;i++)
cout<<temp<<endl;
}
I have a bf class with a member variable "var" whose type is defined as
a template. In bf's constructor, I use (int type) to record what type
var is defined.
The main purpose of bf is to retrieve a double array, then modified it
using the var member.
By different types of var, it does different modifies to the input
array.
There are 3 conditions:
If var is a double( it can be known from "type" variable), it fill the
incoming array with the double value.
If var is a double array, it copies its value one by one into the
incoming array.
If var is a function pointer, it takes the incoming array as the
argument.
There's no problem untill I call bf::eval() in main();
It seems like that if var is a double then the code "var(dPtr, len);"
is wrong. In fact, it is wrong. But that is in case 2. Whenever the var
is not a function pointer, it will never go into case 2 in the switch
in eval();
The familiar situation also happens when the var is set to a function
pointer. The error was found in case 0, in which var is treated as a
double.
I just wanna control the branch call by myself.
Is there any solution?
Thanks