Help C ++ using this C style code

Hey, I'm used to developing in C, and I would like to use C ++ in a project. Can someone give me an example of how I will translate this C-style code into C ++ code. I know that it should compile in a C ++ compiler, but I speak using C ++ methods (Ie Classes, RAII)

 typedef struct Solution Solution; struct Solution { double x[30]; int itt_found; double value; }; Solution *NewSolution() { Solution *S = (Solution *)malloc(sizeof(Solution)); for (int i=0;<=30;i++) { S->x[i] = 0; } S->itt_found = -1; return S; } void FreeSolution(Solution *S) { if (S != NULL) free(S); } int main() { Solution *S = NewSolution(); S->value = Evaluate(S->x);// Evaluate is another function that returns a double S->itt_found = 0; FreeSolution(S); return EXIT_SUCCESS; } 

Ideally, I would like to be able to do something similar basically, but I'm not sure exactly how to create a class, I read a lot of new things, but including it all together correctly seems a little difficult.

 Solution S(30);//constructor that takes as an argument the size of the double array S.Evaluate();//a method that would run eval on Sx[] and store result in S.value cout << S.value << endl; 

Ask if you need more information, thanks.

Edit: changed eval to Evaluate, since I think that eval is a reserved word or at least confusing. In my actual code, I have an array of pointers to functions that I used to evaluate an array of X and store the result in a value. I thought that even they would simply cause extra volume and overshadow my question.

+4
source share
3 answers

Here is one way to do this:

 class Solution { public: // This is the constructor for this class. Note that the constructor // must always have the same name as the class. The line following // the ':' is called the initializer list. It initializes the data // members to a known state. Solution(size_t size) : x(size, 0.0), found(false), value(0.0) { // The 'x(size, 0.0)' is a call to a constructor for the // std::vector class. It creates an array of a size equal to // the first argument, and initializes each element to whatever's // supplied in the second argument (0.0 in this case). } int Evaluate() { // x.begin() returns an iterator that points to the first element // in the array. value = eval(x.begin()); found = true; return EXIT_SUCCESS; } // The 'const' means that this function won't change any (non-mutable) // variables in the class. double GetValue() const { return value; } bool FoundValue() const { return found; } // You may add some more functions to allow users to access/manipulate // the array, if needed. // It is a good idea to keep data members private. private: // Use a vector for resizable arrays. std::vector<double> x; bool found; double value; }; int main() { Solution s(30); // The constructor will be called here. s.Evaluate(); cout << S.GetValue() << endl; } 
+4
source

In C ++, the easiest way to have an array with dynamic size is to use std::vector , since it controls memory allocation and freeing up for you. So, for example, you might have a class like this:

 #include <vector> struct Solution { Solution(unsigned n) : x(n) { } std::vector<double> x; }; 

This gives you a class with a constructor that takes as argument the number of elements to create the double array that we represent with the vector, and initializes the vector with elements n , all of which will be set to zero.

That should at least get you started; I really did not follow what you want to do with the eval function.

+3
source

You don't necessarily need a class in C ++ (for example, unlike Java). You can accomplish this with STL and a free function. Something like lines:

 #include <algorithm> #include <iostream> #include <vector> /// Dynamic "array" of doubles typedef std::vector<double> dvec; /// Free function crunches given numbers double eval_vector( const dvec& d ) { // example: computes the sum return std::accumulate( d.begin(), d.end(), 0 ); } /// entry int main() { dvec v( 30 ); // creates vector of 30 default-initialized doubles std::cout << "result: " << eval_dvec( v ) << std::endl; return 0; } 

What is the meaning of itt_found in the source code? How does your eval() know how many elements are in the array?

0
source

Source: https://habr.com/ru/post/1309892/


All Articles