开发者

Help C++ifying this C style code

开发者 https://www.devze.com 2022-12-30 04:50 出处:网络
Hey I\'m used to developing in C and I would like to use C++ in a project. Can anyone give me an example of how I would translate this C-style code into C++ code. I know it should compile in a c++ com

Hey I'm used to developing in C and I would like to use C++ in a project. Can anyone give me an example of how I would translate this C-style code into C++ code. I know it should compile in a c++ complier but I'm talking using c++ techniques(I.e. 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 (开发者_如何学PythonS != 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 so something like this in main, but I'm not sure exactly how to create the class, i've read a lot of stuff but incorporating it all together correctly seems a little hard atm.

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

Ask if you need more info, thanks.

Edit: changed eval to Evaluate since i think eval is a reserved word, or at least confusing. In my actual code I have an array of function pointers which i was using to evaluate the X array and store the result in value. I thought including them would just cause unnecessary bulk and obscure my question.


Here's one way to do it:

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;
}


In C++, the easiest way to have a dynamically-sized array is to use a std::vector, since it manages memory allocation and deallocation for you. So, for example, you could 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 its argument the number of elements to make the double array, which we represent using a vector, and initializes the vector with n elements, all of which will be set to zero.

This should at least get you started; I didn't really follow what you want to do with the eval function.


You don't necessarily need a class in C++ (as opposed to Java, for example.) You can accomplish this with STL and a free function. Something along the 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's the point of itt_found in the original code? How your eval() knows how many items are in the array?

0

精彩评论

暂无评论...
验证码 换一张
取 消