Advertisement

Little C++ question

Started by February 25, 2001 01:40 AM
8 comments, last by YPhysicist 23 years, 11 months ago
Here is a snippet of some code I tried to compile: -----------------

classmeteor
{
    int a;
    meteor(int b)
    {
        a = b;
    }
};

meteor x(5); //this worked
meteor y[2](3); //the error was on this line.
------------------ The error the compiler gave me was something like "cannot initiate constructor on arrays." So it's impossible to create an array of objects of a class if that class has a constructor? Isn't there any way to do this at all? And also, what other rules apply when creating arrays of user-defined objects that are different than say, creating arrays of integers? Edited by - YPhysicist on February 25, 2001 2:51:28 AM
John Licatogamecreationlab.com
You can''t construct the objects inside the array in the statement where you declare the array.. You should iterate through the array elements and and construct each one by one, e.g.:

-------------------

meteor y[2];

y[0] = meteor(3);
y[1] = meteor(3);

-------------------

In order for this code to work though, you need a default constructor for the meteor class (i.e. one with no arguments), as you''re not doing a dynamic allocation of the meteor objects - Doing it this way means the elements of y get constructed using the default constructor of meteor, but of course you can recreate them later, as done in the above example.


----------------------------------------
Damian.
Email: damian@gamedevcentral.com
Web: www.gamedevcentral.com
----------------------------------------Damian.Email: damian@returnity.comWeb: www.gamedevcentral.com
Advertisement
alrighty, thanks. Here''s another question though, this one about DirectX. Keep in mind that I don''t know much about structures, I''m used to working with classes and was taught structs were evil:

When you create a LPDIRECTDRAW7 object, it''s a pointer to an IDIRECTDRAW7 interface. Does that mean that by creating the object you automatically create the object it is pointing to?
John Licatogamecreationlab.com
An interface is just a somewhat glorified v-table. When you say something like:

  LPDIRECTDRAW7 lpDD;  


You are allocating a pointer to an interface. LPDIRECTDRAW7 isn''t a structure, it''s a typedef to (IDirectDraw7 *). The pointer is meaningless until you call DirectDrawCreate or CoCreateInstance.
In regard to "structs are evil", I would mention that in C++ structs and classes are (virtually) identical.

The _only_ difference is that a class defaults to "private:" members and a struct defaults to "public:" members.

So the following types are identical in use.

class aclass {
public:
int amember;
};

and

struct astruct {
int amember;
}

as well as

class aclass {
int amember;
}

and

struct astruct {
private:
int amember;
}

Structs can have member functions and everything else a class can have. In short, structs are only as evil as a class with a "public:" at the top.
Man, you guys post hundreds of messages a second here or something! I posted my message, went to school, and when i got back my post was already by the bottom of the page! Crazy!

Anyway, thanks, guys, for your responses. One more question though. In some programmer''s code, I saw something like:

struct bla_typ
{
int stuff;
} BLA, *BLA_PTR;

what''s that stuff by the closing bracket?
John Licatogamecreationlab.com
Advertisement
allows you to access the struct as a pointer
bad!
Its just declaring two variables of type bla_typ. Another, equivalent way of writing it would be:

struct bla_typ
{
int stuff;
}

bla_typ BLA, *BLA_PTR;

/* or C-style: */
/* struct bla_typ BLA, *BLA_PTR */

I also suspect that it wasn''t quite as your code says, it was amost certainly a typedef so that the structure and a pointer to the structure could be given more type names:

typedef struct bla_typ
{
int stuff;
} BLA, *BLA_PTR;

Which isn''t a variable declaration, it''s a typedef.



Harry.
Harry.
quote:
Original post by YPhysicist

Here is a snippet of some code I tried to compile:
-----------------
classmeteor{    int a;    meteor(int b)    {        a = b;    }};meteor x(5); //this workedmeteor y[2](3); //the error was on this line. 

------------------

If you need to make an array of meteors, you could declared it like so:
meteor y[2] = { meteor(3),
meteor(3)
};

therefore, you call the constructor for each subscript in the array.

The error the compiler gave me was something like "cannot initiate constructor on arrays." So it''s impossible to create an array of objects of a class if that class has a constructor? Isn''t there any way to do this at all? And also, what other rules apply when creating arrays of user-defined objects that are different than say, creating arrays of integers?

Edited by - YPhysicist on February 25, 2001 2:51:28 AM


In C++ the typedef is automatically supplied if there''s a postfix identifer on a struct declaration. C++ also automatically typedefs the tag as itself, so you can use Bla_typ without putting the struct in front of it (which is required in C without a typedef).

I usually see ''em declared similarily:
  typedef struct tagBla{int stuff;} BLA, *PBLA;  

It''s a legacy declaration from C, which is often desirable if you want to be able to use the same code with both C & C++ compilations.

I suppose all that would work for class too... never tried that though.


Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement