Does this work?

Started by
12 comments, last by Dave Hunt 17 years, 10 months ago

typedef float Matrix[4][4];

typedef Uint8 Uint24[3];


class Example {
   int someVariable;
   Example(int someParameter)
   {
      someVariable = someParameter;
   }
}

. . .

Example *classArray;

. . .

classArray = new Example(32)[8];

Does that work? I don't know, therefore I use the vector, like this:

#include <vector>
std::vector<Example *> classVector;

Advertisement
typedef float Matrix[4][4];typedef Uint8 Uint24[3];

Yes. (Actually I was surprised after trying it; I haven't thought that it is working :)

class Example {   int someVariable;   Example(int someParameter)   {      someVariable = someParameter;   }}

Yes, but is useless since all is private.

classArray = new Example(32)[8];

No. AFAIK only default c'tors are possible with arrays.
If you fix the "all your members are private" problem, then:
std::vector<Example> v;v.resize(32, Example(32));

does something like what you want.

I would also advise for you to look into the boost array classes.

About private: it was just an example.

And about structs, if one don't typedef, Must one always write the keyword struct before in variable declarations?

How I write struct's with typedef:
typedef struct {   int someInt;   char *someString;} Example;Example *pStruct;  // variable declaration


Without typedef:
struct Example {   int someInt;   char *someString;};// variable declarationExample *pStruct; // can one write like this without struct?struct Example *pStruct; // or must one write like this?
Do you guys know if that works, or must I write struct in variable declarations?
No, once you use typedef, you no longer need struct - in C.

In C++, you don't even need typedef. struct is identical to class, but in a way that is backward compatible with C - ie, it defaults to public access and public inheritance.
But in C I must write struct before a struct variable if I write the struct a this:
struct Example {   int someInt;   char *someString;};// variable declarationExample *pStruct; // Works in C++, but not in C?struct Example *pStruct; // In C I have to, in C++ I don't have to?


The question was if I must write struct before in struct variable declarations?
Confirm:
In C I must?
In C++ it works without?

Quote:Original post by Anonymous Poster
The question was if I must write struct before in struct variable declarations?
Confirm:
In C I must?
In C++ it works without?


Yes.

Stephen M. Webb
Professional Free Software Developer

Can one destruct a derived's class it's base class?
Can one construct a new instance in the same variable after destructing a class?

SomeClass classVar(76,"87jh");
delete classVar;
classVar(564"whgft");

This topic is closed to new replies.

Advertisement