Archived

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

Rasmadrak

Class problems

Recommended Posts

Hi there! I´m wondering if someone could help me with a problem I´m having. class thing : public weapon { public: //list of integers,floats,functions etc.. private: }; class weapon { public: //list of integers,floats,functions etc.. void GiveWeapon(int nr); private: }; //instead of doing this.... void weapon::GiveWeapon(int nr) { Strength = WeaponSystems[nr]->Strength; Range = WeaponSystems[nr]->Range; ReloadTime = WeaponSystems[nr]->ReloadTime; TimeToReload = WeaponSystems[nr]->TimeToReload; Type = WeaponSystems[nr]->Type; MaxAmmo = WeaponSystems[nr]->MaxAmmo; CurrentAmmo = WeaponSystems[nr]->CurrentAmmo; Name = WeaponSystems[nr]->Name; }; //I would want something similar to this... void weapon::GiveWeapon(int nr) { "self" = WeaponSystems[nr]; }; ...where "self" is a member of "thing", i.e weapon... Hope someone understands what I mean... =) Thanx for any help!! regards; Robert "Game Maker For Life, probably never professional thou." =)

Share this post


Link to post
Share on other sites
the way that you have it now is that weapon is a super class and thing class derive from weapon. What you want is to make thing the base class then have weapon derive from it, then you can do "self" = weapon object.

Share this post


Link to post
Share on other sites
oh... wait...


doh!!

:D




everything went fine once I started using my brain... :D
I just removed the "=" and it worked...

thanx anyway!! :D

Share this post


Link to post
Share on other sites
hehe...

I must be dehydrated or something today... just because it compiled doesn''t mean it works, right? =S


well, back where I started...
any ideas?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

Again, like nhatkthanh said, in your example, ''weapon'' is your base class, and you have ''thing'' inheriting from it. When you''ve got this:

class thing : public weapon

''thing'' is going to inherit from ''weapon'', not the other way around, which is what it sounds like you''re trying to do.

If I understand correctly (and I may not be), you''ll want to replace "self" with "this". The object that calls GiveWeapon will then be pointing to the WeaponSystems[nr] object. In this case, any changes that are made to index nr of WeaponSystems will also change the object that called GiveWeapon. This is because both are pointing to the same memory location.

Share this post


Link to post
Share on other sites
I got this....



class thing :
public weapon,
public body
//...
{
//....

};


Isn't thing the main class then?? =/



Yes, I believe that is what I want to do... but when I type "this = WeaponSystems[nr];" is says "Assignment to 'this' is not allowed, use X::operator new instead"


[edited by - Rasmadrak on March 20, 2004 12:41:38 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m afraid not. This:

class thing :
public weapon,
public body

will cause ''thing'' to inherit from both ''weapon'' and ''body''. I believe what you''re trying to do is:

class thing
{
// base properties for all objects
}

class weapon : public thing
{
// properties for weapons
}

class armor : public thing
{
// properties for armor
}

Here, ''thing'' will be your base object, and should contain everything that will be common for every type of item (location, weight, etc.). Then, more specific item types (such as weapons or armor) will inherit from ''thing'', and contain all of ''thing''s properties.

Share this post


Link to post
Share on other sites
Ok.. =)

but i want all items to be "things", since then I could use


for (int i=0; i < 10000;i++)
Things[ i ]->Draw();


I found out that I can use following code instead;

void weapon::GiveWeapon(int nr)
{
WeaponStrength = WeaponSystems[nr]->WeaponStrength;
WeaponRange = WeaponSystems[nr]->WeaponRange;
WeaponReloadTime = WeaponSystems[nr]->WeaponReloadTime;
WeaponTimeToReload = WeaponSystems[nr]->WeaponTimeToReload;
WeaponType = WeaponSystems[nr]->WeaponType;
WeaponMaxAmmo = WeaponSystems[nr]->WeaponMaxAmmo;
WeaponCurrentAmmo = WeaponSystems[nr]->WeaponCurrentAmmo;
NameWeapon = WeaponSystems[nr]->NameWeapon;
};


this works, but is more awkward than the version I had in mind.... if I add a variable to the weapon class, I have to add the variable in the "GiveWeapon()" too.... well well, not a big problem.. just awkward.. =)


thanks for your time anyway!!




[edited by - Rasmadrak on March 20, 2004 1:03:25 PM]

Share this post


Link to post
Share on other sites
You can still do

for (int i=0; i < 10000;i++)
Things[ i ]->Draw();


With this method...

Look more into class inheritance and you should understand why Weapon and Armor should inherit from Thing rather than the other way around.

Share this post


Link to post
Share on other sites
Kaezins right, your current system has ''thing'' as a subclass of ''weapon'', not the other way round. Youd definitely want it to be:

class thing
{
// common members to all classes
// + common virtual functions (eg:virtual void Draw())
}

class weapon: public thing
{
// implementation
}

Share this post


Link to post
Share on other sites
Ok, =)

I think I got this now...
Right now I got it like this...

class physics
{
public:
//lots of physics stuff
private:
};

class things :
public physics
{

weapon *Weapon;

};



will it be possible to change the weapon by doing this:

Things[ i ]->Weapon = ListOfWeapons[ 5 ];

?

I''m not at home right now, so I cant check it myself...
Eager to know! :D

thanx for your help! I''m a total newb to classes, especially with inheritance and such... I''ve been using structs before.. =)

/Robert

"Game Maker For Life, probably never professional thou." =)

Share this post


Link to post
Share on other sites