Thread: Struct variables initialised?

  1. #16
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Or you may see this using the initilization list. Doing it in the body is assignment. Data members are initilized prior to the ctor's body; although not true for built in types as there is no guarantee it was initilized to anything. For non built in types the default ctor is called first if you do not do it yourself in the initilization list, then assignment occurs in the body.

    Code:
    class SearchArea
        {
          public:
                 int Fscore;                               //total value to asses select node or not
                 int Gscore;                         //movement cost
                 int Hscore;                                //heurestic             
                 int Walkstate;                           //is it valid terrain
                 int Liststate;                           //is it available? on open or closed list             
                 int STnode;                               //is it a / the search start point 
                 int TGnode;                              //is it a / the search target point             
                 int Xcord;                                 // map node X (across) location
                 int Ycord;                                  // map node Y (down) location
                 SearchArea() : Fscore(0), GScore(0)....etc
                 {  }
          };
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    @C_ntua: every time I see text highlighted red I hover over it to see if it's a link or not. Which is why I prefer to highlight in blue.

    I suppose I should mention that if you're creating a class out of this, at least consider not making all of the member variables public; i.e., use encapsulation (for the record, that is in fact a link).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Muhahaha! Another victim of my red highlight

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by whiteflags View Post
    You can't really use constructors with arrays anyway. I don't really see a problem with what you have, besides how it looks like you could use some bool variables. Is something confusing you?
    Since someone already pointed out my obvious blunder today, I'm going to point out that you absolutely can use a constructor with an array. It's called the default constructor; the one that takes no arguments!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I knew that, but it's just such a painful and stupid restriction.

  6. #21
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by whiteflags View Post
    I knew that, but it's just such a painful and stupid restriction.
    Why is it stupid? Even if the language supported it, you'd be restricted to a single argument list, and all the objects would be initialized identically.

    In my experience, objects that do not have default constructors are not the sort of objects you would have in an array in the first place.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #22
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    thanks to all for input, especially C_ntua, your examples have really helped me nail this method, i will post my 'before' and 'after' versions of my initialisation later this morning, if i can avoid doing my actual job for long enough that is ;->

  8. #23
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    initialisation makeover

    This... >>>

    Code:
       class SearchArea
        {
          public:
                 int Fscore;         // total value to asses select node or not
                 int Gscore;         // movement cost
                 int Hscore;         // heurestic
                 int Walkstate;      // is it valid terrain
                 int Liststate;      // is it available? on open or closed list
                 bool Nodestate;     // is it available for use?
                 bool STnode;        // is it a / the search start point
                 bool TGnode;        // is it a / the search target point
                 int Xcord;          // map node X (across) location
                 int Ycord;          // map node Y (down) location
                 int ParentPOS;      // point to parent node
    
                 SearchArea()
                 {
                  Fscore = 0;
                  Gscore = 20;
                  Hscore = 0;
                  Walkstate = 1;
                  Liststate = 2;     //Neutral until node is accessed then 0 or 1
                  Nodestate = true;
                  STnode = false;
                  TGnode = false;
                  Xcord = 0;
                  Ycord = 0;
                  ParentPOS = 0;
                 }
    
          };
    means i can get rid of this ! >>

    Code:
    for(dwn = 0; dwn < dwnMax; dwn++)
       {
               for(acr = 0; acr < acrMax; acr++)
               {
                   mapnode[dwn][acr].Xcord = acr;
                   mapnode[dwn][acr].Ycord = dwn;
                   mapnode[dwn][acr].Walkstate = 1;
                   mapnode[dwn][acr].Nodestate = true;
                   mapnode[dwn][acr].STnode = false;
                   mapnode[dwn][acr].TGnode = false;
                  // next membervar
                  // next membervar
                  // blah
                  // blah blah
                  // ad nauseum..
                }
       }
    Magic, i love it! thanks for the advice. i think maybe all the members should be private really.
    Last edited by rogster001; 09-15-2009 at 02:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Replies: 4
    Last Post: 12-12-2002, 02:32 PM