Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 789371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:30:52+00:00 2026-05-14T21:30:52+00:00

Hey I’m used to developing in C and I would like to use C++

  • 0

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 (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 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:30:52+00:00Added an answer on May 14, 2026 at 9:30 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, my Computational Science course this semester is entirely in Java. I was
Hey, I've been developing an application in the windows console with Java, and want
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey so what I want to do is snag the content for the first
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey peoples, I've been studying Java for a couple of weeks, and have decided
Hey right now I'm using jQuery and I have some global variables to hold
Hey, in the Programming Pearls book, there is a source code for setting, clearing
Hey having some trouble trying to maintain transparency on a png when i create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.