Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

programering

this-> in struct . Does this feature work???

This topic is 5386 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a SST (Side Scrolling Tiles) game like mario bros. The map will contain blocks and liquids, they are elements.
struct element_definition
{
    short type;   // block, liquid or surface

    bool animated;  // animated?

    SDL_Surface *image;  // the collection of bitmaps

    SDL_Rect bitmap;  // the clip rect

    BitmapLayer *layer;  // the bitmap layer

};
typedef struct element_definition ElementDefinition;
 
ElementDefinition PreDefinedElements[] =
{
  { // a block element

    _element_block, false,
    GetBitmapSrc(0), {0,0,40,40},
    new BitmapLayer(this->image, &this->bitmap)  // this???

  },
  { // a liquid surface element

    _element_liquid_surface, true,
    GetBitmapSrc(1), NULL,
    new Animator(this->image, Animations[0], 0)
  },
  { // a liquid element

    _element_liquid, true,
    GetBitmapSrc(1), NULL,
    new Animator(this->image, Animations[0], 1)
  }
};
EDIT: The class Animator is derived from the BitmapLayer class. My question is: can I use this-> to access a member within a struct during definition/initializing? You see I don't want to use a pointer/reference from the outside. ------------------------------- Anton Karlsson Klingis Entertainment Games with silly humor Aleph One (Marathon Open Source) | My Homepage Packa bajs med winzip! Just dreaming wont make you (more) skilled in game programming/development. [edited by - programering on September 23, 2003 6:39:23 AM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
... create a constructor for your struct which initializes the BitmapLayer member. you may as well create a destructor, too, and utilize the Resource Acquision Is Initialization idiom.

Share this post


Link to post
Share on other sites
>>... create a constructor for your struct which initializes the >>BitmapLayer member.

Does that work? How do I do?
Does this-> work in a struct initializing?

>>you may as well create a destructor, too, and utilize the >>Resource Acquision Is Initialization idiom.

That last I didn''t understand.

Share this post


Link to post
Share on other sites
In C++, structs and classes are essentially the same thing. A struct can do everything a class can do, and vice versa.

AFAIK the only difference is the default access: members of structs are public by default, members of classes are private.

Share this post


Link to post
Share on other sites
One other struct/class difference; the default access for inheritance. If you do this:

class A {}
class SpecialA : A {}, then SpecialA inherits as private by default.

struct A {}
struct SpecialA : A {}, then SpecialA inherits as public by default.

No, I have never fould this useful as I always specify public or private myself when I inherit, but I just thought I would point it out.

Share this post


Link to post
Share on other sites
How do I access the SpecialA class inheritence members?

But the main question was:
Can I use this-> to access a struct's members within itself during an initialize definition? Yes or No!
Like this, for example:
struct example {short e,*r;};

struct example exmp = {10, &this->e}; // HERE!!!

-------------------------------
Anton Karlsson
Klingis Entertainment
Games with silly humor

Aleph One (Marathon Open Source) | My Homepage

Packa bajs med winzip!

Just dreaming wont make you (more) skilled in game programming/development.

[edited by - programering on September 24, 2003 7:46:56 AM]

Share this post


Link to post
Share on other sites
Hello programering,

Well I look at this agian I belive what your trying to do is not possible, though I have not test it in code very busy actual doing work.

But here some things I can see your trying to use the C way of initializing a struct which is ok if you do it right.

{ // a block element
_element_block, false,
GetBitmapSrc(0), {0,0,40,40},
new BitmapLayer(this->image, &this->bitmap) // this???
}

This is not right at this block of code your not in the struct(class) so the "this" pointer is not defined.
What errors does the compiler tell you?
I hope you try to compile this? so you can tell us what it balks at. And I am sure it will.

Think of it like this your try to call a constructor to a element_definition(_element_block, false, GetBitmapSrc(0), {0,0,40,40}, new BitmapLayer(this->image, &this->bitmap) )

so how is the "this" pointer supose to be set before the struct(class) is even created? it can''t be, so error.

And as the AP said create constructors and since you have a pointer I would create the destructor to.

of course you can rewrite the block as such.
{ // a block element
_element_block, false,
GetBitmapSrc(0), {0,0,40,40},
new BitmapLayer(GetBitmapSrc(0), {0,0,40,40})
}
and it should work fine.

Lord Bart

Share this post


Link to post
Share on other sites
quote:
Original post by programering
Can I use this-> to access a struct''s members within itself during an initialize definition?

Within the scope of the struct itself, yes.
quote:

Like this, for example:
struct example {short e,*r;};

struct example exmp = {10, &this->e};

Where have you been learning C++ from? The syntax you are using is bizarre! I suggest you pick up a decent learner text before attempting to write programs in C++. For starters, your attempt to reference this is not within the scope of an `example'' struct instance, so is not even an example of `accessing a struct''s members within itself'', as you put it.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
Where have you been learning C++ from? The syntax you are using is bizarre! I suggest you pick up a decent learner text before attempting to write programs in C++. For starters, your attempt to reference this is not within the scope of an `example'' struct instance, so is not even an example of `accessing a struct''s members within itself'', as you put it.



I''m not a newbie if you think that!

short e,*r;
is short for:
short e;
short *r;

And this-> was a suggestion, I wasn''t so sure myself it would work.
But if it will fail one should do improvements to the C compilers with a that-> feature of it.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!