Novice: converting C++ code to CLI Form application
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Novice: converting C++ code to CLI Form application

  1. #1
    Join Date
    Jun 2013
    Posts
    2

    Novice: converting C++ code to CLI Form application

    Starting out with Visual C++, so questions may seem rather trivial.

    As a starter project I want convert a Neural net app from "AI Techniques for Game Programming (2002 Buckland)" to a Visual C++ CLI Forms application. I have created the interface and now I have to rewrite the in/output routines, amongst other things.

    First question I have has to do with variable initialization used by Buckland. Code looks like this:

    Excerpt from header file (CNeuralNet.h):

    //-------------------------------------------------------------------
    // define neuron struct
    //-------------------------------------------------------------------
    struct SNeuron
    {
    //the number of inputs into the neuron
    int m_NumInputs;

    //the weights for each input
    vector<double> m_vecWeight;


    //ctor
    SNeuron(int NumInputs);
    };

    Excerpt from .cpp file (CNeuralNet.cpp):

    //*************************** methods for Neuron **********************
    //
    //---------------------------------------------------------------------
    SNeuron::SNeuron(int NumInputs): m_NumInputs(NumInputs+1)

    {
    //we need an additional weight for the bias hence the +1
    for (int i=0; i<NumInputs+1; ++i)
    {
    //set up the weights with an initial random value
    m_vecWeight.push_back(());
    }
    }

    Question I have: what is the the function of ": m_NumInputs(NumInputs+1)" after the method declaration? Buckland does this in many places in his code. In this case it's a struct, but he does it with classes too.

    This is not used in the Visual C++ books I use for learning and besides that, I wonder if there is a more "elegant" or easier to understand way to do this?

    I have more questions, but I'll start with this one.

    Thanks! Jan

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Novice: converting C++ code to CLI Form application

    It's right there in the code

    //we need an additional weight for the bias hence the +1

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,996

    Re: Novice: converting C++ code to CLI Form application

    When you post formatted code, please use code tags. It makes the code much easier to read (Go Advanced, select code, click '#')

    Code:
    SNeuron::SNeuron(int NumInputs): m_NumInputs(NumInputs+1)
    In a class/struct constructor it's a way of initialising a variable. In this case its equivalent to

    Code:
    SNeuron::SNeuron(int NumInputs)
    {
       m_NumInputs = NumInputs+1;
    This : method of variable initialization is common in constructors. Whilst in this case it could be done using a simple assignment, when using derived classes it's the only way to call constructors of the base class(s) in the constructor of the derived class. It's also commonly used to initialise other classes defined as part of the class.

    Code:
    struct test
    {
       std::string mystr;
    
       test(std::string str = "") : mystr(str) {}
    };
    Code:
    class class1
    {
    private:
    	int c1;
    public:
    	class1(int no1 = 0) : c1(no1) {}
    };
    
    class class2 :public class1
    {
    private:
    	int c2;
    public:
    	class2(int no1 = 0, int no2 = 0) 
    		: class1(no1), c2(no2) {}
    };
    What books are you using to learn c++?

    You might like to look at these web sites
    http://www.cplusplus.com/doc/tutorial/
    http://www.learncpp.com/
    Last edited by 2kaud; June 10th, 2013 at 03:16 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Jun 2013
    Posts
    2

    Re: Novice: converting C++ code to CLI Form application

    First post: almost instant feedback! Great, thanks!

    That's what I assumed, it could be done with a simple initialization within the struct. The previous example was still pretty neat, but this gets rather messy (from same book):

    Code:
    //---------------------------------------constructor---------------------
    //
    //	initilaize the sweepers, their brains and the GA factory
    //
    //-----------------------------------------------------------------------
    CController::CController(HWND hwndMain): m_NumSweepers(CParams::iNumSweepers), 
    										                     m_pGA(NULL),
    										                     m_bFastRender(false),
    										                     m_iTicks(0),
    										                     m_NumMines(CParams::iNumMines),
    										                     m_hwndMain(hwndMain),
    										                     m_iGenerations(0),
                                             cxClient(CParams::WindowWidth),
                                             cyClient(CParams::WindowHeight)
    {
    Books: I am very happy with "Programming with Visual C++: Concepts and Projects by Allert (2008)" as from the very first exercises you are working in a Forms environment. It's for absolute Visual C++ beginners though. I am in search of a follow-on as this only touches the very basics. I also have "Ivor Horton's Beginning Visual C++ 2010", but use that more as a look-up reference.

    Next question: Buckland puts all global/instance variables into a separate class (CParams) which he then uses throughout the program CParams::variable, whereas I plan to put them in the windows form designer code section, just below #pragma endregion. I could then do away with the CParams class leader when referencing these vars.

    Correct?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center