String concatenation

Hello,

I have a class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Parameters {
protected:
    double s_a, phi_a;
    double s_b, phi_b;
    double s_c, phi_c;
    double s_d, phi_d;
    double s_e1, phi_e1;
    double s_e2, phi_e2;
    double s_e3, phi_e3;
    std::string cyl[7];
public:
    Parameters();
    ~Parameters();
    
    std::string CylinderName(int i) {
        cyl[0] = "a";
        cyl[1] = "b";
        cyl[2] = "c";
        cyl[3] = "d";
        cyl[4] = "e1";
        cyl[5] = "e2";
        cyl[6] = "e3";
        
        std::string cil = cyl[i];
        
        return (cil);
    }

    double aRadialDistance(){double ar=0.;s_a=ar;return s_a;}
    double bRadialDistance(){double br=0.7;s_b=br;return s_b;}
    double cRadialDistance(){double cr=1.21;s_c=cr;return s_c;}
    double dRadialDistance(){double dr=1.4;s_d=dr;return s_d;}
    double e1RadialDistance(){double e1r=1.85;s_e1=e1r;return s_e1;}
    double e2RadialDistance(){double e2r=1.85;s_e2=e2r;return s_e2;}
    double e3RadialDistance(){double e3r=1.85;s_e3=e3r;return s_e3;}
    
};


and I want to know if it is possible to call what's inside in my main function using string concatenation.

For example,

1
2
3
4
5
6
7
8
9
int main(int argc, char *argv[]) {
   Parameters.parameter;

   for (int k=0;k<6;k++) {   
       std::string L;
       L = parameter.CylinderName(k)+"RadialDistance";
       A.Cylinder(parameter.L());
   }
}


I know the above is wrong because it does not work. But I was wondering how would one go about doing something like that (if it's clear).

Thank You!
This works:
1
2
3
4
5
6
7
8
9
10
11
12
int main(int argc, char *argv[]) {
   // You had a '.' between Paramenters and parameter
   Parameters parameter;

   for (int k=0;k<6;k++) {   
       std::string L;
       L = parameter.CylinderName(k)+"RadialDistance";
       cout << L << endl;
       // I don't know what A and Cylinder are
       //A.Cylinder(parameter.L());
   }
}
aRadialDistance
bRadialDistance
cRadialDistance
dRadialDistance
e1RadialDistance
e2RadialDistance
I don't recommend you to do this but you could use a map that maps strings to function pointers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, char *argv[])
{
	std::map <std::string, double (Parameters::*)()> functionMap;
	functionMap.insert(std::make_pair("aRadialDistance", &Parameters::aRadialDistance));
	functionMap.insert(std::make_pair("bRadialDistance", &Parameters::bRadialDistance));
	...

	Parameters parameter;
	for (int k=0;k<6;k++) {   
		std::string L;
		L = parameter.CylinderName(k)+"RadialDistance";
		A.Cylinder((parameter.*functionMap[L])());
	}
}
Last edited on
Not like this
Parameters.parameter;
but like this
Parameters parameter;

what is A in 7 line?
I'm sorry. I should have explained that. A.Cylinder is another class that asks for a double. So I have something like this,

1
2
3
class Anything() {
   double Cylinder(double) {code here}
};


So my main is,

1
2
3
4
5
6
7
8
9
10
int main(int argc, char *argv[]) {
   Parameters parameter;
   Anything A;

   for (int k=0;k<6;k++) {   
       std::string L;
       L = parameter.CylinderName(k)+"RadialDistance";
       A.Cylinder(parameter.L());
   }
}


I want parameter.L() to return the corresponding double in Parameters. But it can't be done the way I did it above.

Thanks!
Last edited on
Peter87, why won't you recommend doing this?
I guess I just don't like that functions are looked up by strings during runtime. Mostly because it's unnecessary slow.

Can't you have a function like CylinderName that instead executes the correct function depending on the number you pass in. That way you don't have to care about the function names.

If you really need function lookup by name I guess my solution is not that bad.
Last edited on
I guess using map will do it for now since I have to do some sort of permutation with about 14 of them. Thank You!
Topic archived. No new replies allowed.