Custom Classes and Pointers

Started by
8 comments, last by Si0n 19 years, 3 months ago
I am moving from Game Maker to C++, and for my first game, I'm trying to make the syntax slightly similar to cut down on confusion. I have a custom class called 'WSprite', and another class (as an object) called 'objX'. In 'objX', I declared an array of two sprite pointers, and try to assign them to certain sprites, like this:
WSprite sprX, sprXs;

class objX {
 ...
 WSprite *spr[2];
 ...
 void Create();
};

void objX::Create() {
 ...
 *spr[0]=&sprX //Trying to assign pointer locations
 *spr[1]=&sprXs
 ...
}
And I get this error with Dev-C++ 4.9.9.1: " 74 F:\Misc\C++\DirectDraw\X.h no match for 'operator=' in '*this->objX::spr[0] = &spr_X' " " error F:\Misc\C++\DirectDraw\ddstuff.h:78 candidates are: WSprite& WSprite::operator=(const WSprite&) " (the same for spr[1].) As you can see, the only option it gives me is creating new WSprite's, which I don't want to do... Is there some way to fix this?
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Advertisement
If you are trying to put pointers to sprites in the array then just remove the leading * in the assignment lines so your function would look like this:
void objX::Create() { spr[0]=&sprX //Trying to assign pointer locations spr[1]=&sprXs}


EDIT:
The * dereferences the pointer, so when you assign a pointer to it it is trying to assign a pointer to an instance.
Okay, now I'm a little confused... I thought that in arrays, the dereference for the * operator was switched. See, when I'm editing a DirectDraw surface after unlocking it, this is what it's like (in psuedo code):
BYTE *pixels;pixels=(BYTE*)UnlockDD(lpDDBack); //Psuedofor (i=0;i<=Width*Height*Depth-1;i++) pixels=0;

So what is *pixels? And what's the difference between that and this:
(BYTE*) pixels[Width*Height*Depth-1];*pixels[0]=255;

Aw crap, I'm really confused...
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Not sure if this is what you are aksing but
let's say you have BYTE *pixels[3];

*pixels and pixels[0]
*(pixels + 1) and pixels[1]
*(pixels + 2) and pixels[2]


are the same...
Ah, I'm beginning to understand.... Sort of. Well, at least I have my C++ book.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
the indexation is just a shortcut for the pointer addition method that I showed you.

since arrays are groups of objects in memory consequently (blergh, I don't even know if that is a word), they are like a line.

so if you say *(pixel + 2)

You start at the beginning of the line, then you move two, and you get the person at spot two (u just gotta remember that this line starts at zero =P)

and pixel[2] is just a nice shortcut.
Quote:Original post by Si0n
since arrays are groups of objects in memory consequently (blergh, I don't even know if that is a word), [snip...]

[nitpicking mode engaged...]
Consequently is a word, it just doesn't mean what you want it to mean ;)
The word you're looking for here is contiguously.

- Neophyte

[Edited by - Neophyte on December 30, 2004 8:38:54 PM]
Making better sense. Thanks.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
The * is a part of the variable's type, not its name.

"WSprite *spr[2]" is an array of two WSprite*'s. That is, it contains two elements, each of which is a WSprite *. Thus "spr[0]" is a WSprite*, and so is "spr[1]". (And "*spr[0]" is a WSprite, since you dereferenced it).

You want the pointers in the array to point to the WSprites, so you just want to assign those addresses.

The error message you get has nothing to do with creating new sprites. The compiler is saying:

" 74 F:\Misc\C++\DirectDraw\X.h no match forI don't know how to 'operator='assign in '*this->objX::spr[0] = &spr_X' "&spr_X" to "*spr[0]" "

" error F:\Misc\C++\DirectDraw\ddstuff.h:78 candidates are:I'm trying to be psychic here (and I'm usually wrong), but were you trying to use WSprite& WSprite::operator=(const WSprite&)assignment from a WSprite to another WSprite?"

It makes this guess because it's reading left to right, and the first thing it sees is a WSprite (not WSprite*), and then it sees a legal way to assign a WSprite to a WSprite, but it doesn't (and shouldn't!) see a way to make a WSprite equal to a WSprite*.

For your other example, "BYTE * pixels" is just a pointer to a BYTE, while "(BYTE*) pixels[Width*Height*Depth-1]" is an array of such pointers.

There are only two real tricks here:

1) A pointer to "a thing" can be interpreted as a pointer to a sequence of things, of unknown length. Since a pointer is just a memory address, we think of a pointer to the first thing (with all of them appearing one after another in memory) as pointing to all of them. However, the pointer has no idea how many things there are. In general it is not safe to assume more than one. (This is one of the many reasons you should prefer std::string for text representation).

2) An array name, without any subscript, is shorthand for a pointer to the first element of the array, which can therefore be thought of as a pointer to all of the array contents. Here there *does* exist information about the number of things, but really only at compile-time - it is easy to lose that information. (Which is one good reason to prefer std::vector over plain arrays, although this is IMHO not as important as using std::string for its purpose).
Quote:Original post by Neophyte
Quote:Original post by Si0n
since arrays are groups of objects in memory consequently (blergh, I don't even know if that is a word), [snip...]

[nitpicking mode engaged...]
Consequently is a word, it just doesn't mean what you want it to mean ;)
The word you're looking for here is contiguously.

- Neophyte


I stand corrected. =)

This topic is closed to new replies.

Advertisement