what do they mean by zeroing out default constructor? what it does? why its good? and how do i do it?
what do they mean by zeroing out default constructor? what it does? why its good? and how do i do it?
Perhaps they meant "zeroing out in the default constructor" or something like that, i.e., implementing the default constructor to initialise member variables to zero.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Like this?
Code:class X { int a, b; public: X(): a(0), b(0) {} };
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
What did you try?Originally Posted by ExDHaos
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Code:Races(): raceIDnu(0), racePowe(0), raceName(0), raceEner(0), racePowP(0), raceAttr[0](0), raceAttr[1](0), raceAttr[2](0), raceAttr[3](0), raceResE[0](0), raceResE[1](0), raceResE[2](0), raceResP[0](0), raceResP[1](0), raceResP[2](0), raceResM[0](0), raceResM[1](0), raceResM[2(0){}------ Build started: Project: Helios RPG - Beta, Configuration: Release Win32 ------
Compiling...
Main.cpp
c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Races.h(27) : error C2958: the left bracket '[' found at 'c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\races.h(27)' was not matched correctly
c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Races.h(25) : error C2059: syntax error : '['
Build log was saved at "file://c:\Documents and Settings\eng\My Documents\Visual Studio 2008\Projects\Project1\Helios RPG - Beta\Helios RPG - Beta\Release\BuildLog.htm"
Helios RPG - Beta - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Ah, so you are trying to zero initialise an array member variable. You should be able to use value initialisation, if I am not wrong:
Code:Races() : raceIDnu(0), racePowe(0), raceName(0), raceEner(0), racePowP(0), raceAttr(), raceResE(), raceResP(), raceResM() {}
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Please show the code.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
latter i want to rep;ace the arrays with vectorsCode:#include <string> using namespace std; #define MAX_RACES 3 #define SK charRace->racePower #define IDr charRace->raceIDnu #define MANA_S 1 #define SPIRIT_S 2 #define ANCIENA_S 3 class Races { public: int raceIDnu; int racePowe; string raceName; string raceEner; string racePowP; float raceAttr[4]; float raceResE[3]; float raceResP[3]; float raceResM[3]; Races() : raceIDnu(0), racePowe(0), raceName(0), raceEner(0), racePowP(0), raceAttr(), raceResE(), raceResP(), raceResM(){} void raceInfo (int iValue[], string sString[], float dValue[]) { raceIDnu = iValue[0]; racePowe = iValue[1]; raceName = sString[0]; raceEner = sString[1]; racePowP = sString[2]; raceAttr[0] = dValue[0]; raceAttr[1] = dValue[1]; raceAttr[2] = dValue[2]; raceAttr[3] = dValue[3]; raceResE[0] = dValue[4]; raceResE[1] = dValue[5]; raceResE[2] = dValue[6]; raceResP[0] = dValue[7]; raceResP[1] = dValue[8]; raceResP[2] = dValue[9]; raceResM[0] = dValue[10]; raceResM[1] = dValue[11]; raceResM[2] = dValue[12];} }racesDB[MAX_RACES], *charRace; void LoadRaces() { int iValue_0[] = {1, MANA_S}; string sValue_0[] = {"Haijemarian", "Spirit", "SP"}; float fValue_0[] = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; racesDB[0].raceInfo(iValue_0, sValue_0, fValue_0); int iValue_1[] = {2, SPIRIT_S}; string sValue_1[] = {"a", "a", "a"}; float fValue_1[] = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; racesDB[1].raceInfo(iValue_1, sValue_1, fValue_1); int iValue_2[] = {3, ANCIENA_S}; string sValue_2[] = {"a", "a", "a"}; float fValue_2[] = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5}; racesDB[2].raceInfo(iValue_2, sValue_2, fValue_2); }
...maybe not....latter i want to replace the arrays with vectors
First off, there's no need to 'zero' out the strings. They'll be handled already by the simple fact that they're strings from STL, and their default initialization is already such. (In fact, supplying zero in this manner causes an error in my IDE).
Also, the initial state of a vector will be 'empty' if you choose to use them, and you'll need to push values into the array to 'get them started' - but, you can't do that with the initialization syntax, you'll need to push those floats in the constructor.
An array of floats can be quite simple, and if the bounds of the array are very clear in your code, the speed of a simple array over a vector (for initialization and simple storage) is hard to resist in this context. It's one of the rare contexts in which arrays can make more sense than vectors (fixed length arrays, private members that the owning object can 'check' for bounds, etc).
It generally is better to use vectors.
The trick is to recognize a situation where a simple array MIGHT be a better choice.
Let's analyze a little more about why (and why not).
You have a few entries in each array (4 at most now, but even if it were 8 or 10, it's a small quantity).
The storage is part of an object; you're not dynamically allocating the array, you're using a fixed size within the object, which itself will be destroyed, taking the array(s) with it.
The arrays should be private members (they're not in your case, yet). By making them private members your object can control access in such a way that reaching beyond the limit of the array may be prevented.
Counter that with the use of vectors.
The vector is going to start out with zero entries. You can choose to reserve a few space, but that will invoke dynamic memory allocation (taking time), and you'll have to initialize with push_back to create sufficient entries in the vector.
When the owning object is destroyed, it will take some time for the vector to be destroyed (a delete will have to be called on the dynamic space allocated).
This is a time/convenience compromise. In most circumstances, the array's safety and it's own kind of convenience would win out, and be a better choice. If the size of the array were not fixed, or if the size were larger, or if the storage of the array was dynamically allocated (off a pointer) instead of part of a large class, the vector is clearly superior.
This is not true since one could just initialise the vectors to be of the desired size.Originally Posted by JVene
EDIT:
The problem of array bounds still applies here. For example, the raceInfo() member function assumes that the arrays referenced by its pointer parameters have at least three or four elements, but a mistake by the caller could then lead to array out of bounds access.
std::tr1::array can help alleviate this problem by allowing you to have fixed size arrays that know their own size, with no or almost no overhead over using arrays directly.
Last edited by laserlight; 05-17-2009 at 12:26 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)