Arrays with Structs and constructors !?

Started by
14 comments, last by stylin 18 years, 5 months ago
An additional point on consistency:

For user-defined types, it is wise to emulate the functionality of built-in types where possible (this especially applies to types that can represent numbers). For operator -, consider the following:
   tvector3 v1( 1, 2, 3 ), v2( 4, 5, 6 ), v3( v1-v2 );   ...   if( v1-v2 = v3 )   // oops, meant to do a comparison (==)      ...

The difference is subtle here, and most compilers will flag the conditional on tvector3 because of the (incorrect) attempted conversion to bool. For built-in types, this code of course would not compile. But say you have a rational number class, for instance, you'd probably want an implicit conversion to float, which as a built-in type has an implicit conversion to int, which has an implicit conversion to bool. In this case the above typo would compile and give you extremely erroneous results.

To solve this problem at compile-time, not only will you return an object by value from operator-(), but you will also make that return value const:

const rational operator -( const rational & );

This prevents debugging headaches by not allowing the "I meant conditional instead of assignnemt" to compile. This applies to all types implicitly convertible to int or bool, but again for consistency's sake, it's a good habit to get into.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Advertisement
All right, I still get the main idea of a protected pack of data, and the external traces access of public ones through structures and classes…all that even made me think ‘bout a compression decompression program and his coding and decoding functions making more comfortable the inner call of each other structures in classes driven by the result of an another structure… Too much confusion in the rules of different declarations in classes although in structures to make an exploitable example right here…Like calibrating on the machine speed (clk tck) to have the most optimized chain of functions… still counting on errors to discover the rules

Beside, I’d really like to learn how to make one or several arrays from these structures we’re talking ‘bout…does anyone?
Since your structs all have default constructors and the default copy assignment operator does the right thing you can declare and use an array of them just like you would any builtin type:
Vector3 stackArray[42];int x = 42;Vector3 * dynamicArray = new Vector3[x];std::vector< Vector3 > saferDynamicArray(x);stackArray[7] = dynamicArray[8] = saferDynamicArray[9] = Vector3(7, 8, 9);delete[] dynamicArray;

Enigma
Quote:Original post by jinuuchukuu
After that most important if that somewhat is known how to declare and use arrays with that type of typedefStruct pleaseYourself !!

Not quite sure what you're asking, but you can create arrays of classes like this:
The hard way:   const unsigned int array_size = 1000;   // unsafe static array   tVector3 unsafe_static_array[ array_size ] = { tVector3() };   // unsafe 'dynamic' array   tVector3 * unsafe_dynamic_array = new tVector3[ array_size ];

The easy way:
   #include <vector>   const unsigned int array_size = 1000;   // safe array that grows dynamically   std::vector< tVector3 > safe_array1;   // safe array with initial size that grows dynamically   std::vector< tVector3 > safe_array2( array_size, tVector3() );

EDIT: beaten.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Not only I couldn’t stay to wait for an answer,(hope there will still be someone looking here) I couldn’t afford waiting to answer you before I have to try what you guys tell me… but in the compiler vc++ 6 none of the declarations seems to be token in consideration especially when I use
stackarraytPlanX[iter].a.x =stackarraytPlanX[iter]->a.x =

Making those declarations in a class declaration in a header file…
WHAT HAPPENS! I never fall on good source text about that subject maybe because I don’t pass the beginner’s cap for some reason… does someone know about a great book for c++ with great explainations ?
Anyway,
Quote:from StylinThe easy way:
#include <vector>
const unsigned int array_size = 1000;
// safe array that grows dynamically
std::vector< tVector3 > safe_array1;
// safe array with initial size that grows dynamically
std::vector< tVector3 > safe_array2( array_size, tVector3() );

i guess those safe ways doesn't work declared in classes...
Quote:Original post by jinuuchukuu
i guess those safe ways doesn't work declared in classes...

It might be a communication problem, but I have a hard time understanding what you are asking. The [] operator for a std::vector works like a standard array, ie., it returns a reference to the index you pass to it.
   // meaningless shape class for demonstration   class shape { public: void draw() {;} };   // create a vector of 420 default shapes   std::vector< shape > shape( 420, shape() );   // access those shapes using operator[]   for( int i=0; i<shapes.size(); ++i )      shapes.draw();            // shapes returns a reference to the ith element   // access those shapes using an iterator   std::vector< shape >::iterator it = shapes.begin();   while( it != shapes.end() ) {      it->draw();                  // it points to a particular element      (*it).draw();                // so we can dereference the iterator as well      ++it;                        // point to the next element   }

In your code, it looks like you're trying to pass an iterator to operator[]; if you have an iterator, you can just dereference it to gain access to the element it points to. To use [] notation, just pass an integer index (exactly like you do for arrays) and that will return a reference to that element.

Let me know if this is still unclear.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid

This topic is closed to new replies.

Advertisement