Thread: Hard to get classes

  1. #31
    Registered User
    Join Date
    Nov 2001
    Posts
    53
    Something like this (using my previous stupid example ) :


    Code:
     
    
    #include <iostream.h>
    #include "MonsterClass.h"
    
    MonsterClass *create_monsters();
    MonsterClass MyMonsters[10];
    
    void main(void) {
       
       MonsterClass *MPointer;
       MPointer=create_monsters();
           
       cout << MPointer->getname() << endl;
       cout << (MPointer+1)->getname() << endl;
    
    }
    
    MonsterClass *create_monsters() {
       MyMonsters[0].setname("Goggle Eyes");
       MyMonsters[1].setname("help me!!");
       return(&MyMonsters[0]);
    }
    Does that make it more obvious?

    Ah.
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  2. #32
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Thanx...

    Yes I think I got it now Thanx, though I have a nother question, they never stop comming do they

    Im trying to do this project that Im working on with classes and everything that comes with that, but something is really annoying here. I have this class Character, I start with creating an object of that class, and as i see it I want to use **this** object from the start until the end of my program.

    How do I do this, if I want to access my object, lets say in another class.

    I have tried this:

    class Character
    {
    // what ever
    }c;

    But then the compiler starts to complain.

    How will I do this? for I need to be able to call the same object in different classes..

  3. #33
    Registered User
    Join Date
    Nov 2001
    Posts
    53
    Ok... let me just get this straight before I reply fully... you want to create an object (of a class type, if you like) and be able to access that from within other classes. Does that mean you want to be able to access the methods etc. from your Character object from within the methods of other objects?

    Give me an example of the object you are trying to access your character from and i'll help.. I think I know what you mean but need to be sure here....

    Almosthere
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  4. #34
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Something like this...

    Ok, I am a bit confused myselfe for the moment on what I am doing, because I am testing what can be done, and what canīt, and what the best way of doing what I am doing is, but This is the problem:

    I have a class where I need to get the characters coordinates
    I then need to use the object that I have

    Class Adventure
    {
    void walking(); // need to get character coordinates for this
    // and some more functions
    };

    I also have another class calles fight
    I will need some values from the Character class object here
    class Fight
    {
    void characterAttack();
    void enemyAttack();
    };

    I made this short, because I dont really know how I will have it yet, But you where right when asuming that I wanted to be able to call for an object of a class in another class, I dont know if this is such a good way of doing it, how would you do it? would you have all methods that needed to use the same object in one file.

    dont know if this makes any sense!!

  5. #35
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    in the adventure class
    void Adventure::walking()
    {
    // I need to have an object of character
    Character c;
    int x,y;
    x = c.getXcoord();
    y = c.getYcoord();
    }

    in the class Fight

    void Fight::characterAttack()
    {
    Character c;
    int chDefens;
    // But if I create a object here to, it will not be the same one
    // And so totally useless to me
    chDefens = c.getCharArmor();
    }

    // Another short example, just to show what I would want!!
    // This isnt how I have written my program (just an example)

  6. #36
    Registered User
    Join Date
    Nov 2001
    Posts
    53
    Ok... i think i follow ya...

    What you want to be doing is passing a pointer to the function which needs the object (pointers are just references, right, so if you need to reference the object you need a pointer to it).

    For example, your method 'characterAttack' needs to have a pointer to the character object you want to do the attacking (if that's the object you want to use). Roughly like this :

    Code:
    // Declare somewhere
    
    Fight MyFight;
    Character MyChar;
    
    // Call something like this :
    
    MyFight.characterAttack(&MyChar);  // Send the address of your character object
    
    // And the function itself.... a CharacterClass pointer parameter which you use to pass the object (as per above)
    
    void Fight::characterAttack(CharacterClass *attacking_char) {
       cout << "Attack underway by " << attacking_char->getname();       
    }
    Do you see? This way the functions you use can be re-used if they are re-usable. You will prob. want to use some inheritance so that both the monsters and character share all the characteristics needed to allow them to fight - then alter the function so that it uses the "base" class they are both inherited from rather than their specific classes. This way monsters AND characters can fight (get it? all characters, monsters, animals etc. have positions in the world, statistics etc. and should be allowed to fight!! let them all fight!!!!!!! )

    Hmm. Anyway, keep the code as generic as possible by passing pointers to the most basic object-types those functions need to perform their operations.

    Don't forget, the whole point of classes (as far as I see it anyway ) is to use them for GOOD not evil. Erm. I mean, use them lots rather than as a one off.

    Almosthere
    "Technology has merely provided us with a more efficient means for going backwards" - Aldous Huxley

  7. #37
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Thanx I understand..

    Thanks, It was really nice of you taking the time to show me how to do this. I am rather sure that I will have more questions soon though, I really dont get far in my coding before they come

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-06-2005, 07:11 PM
  2. Abstract classes
    By cisokay in forum C++ Programming
    Replies: 17
    Last Post: 05-29-2005, 09:39 AM
  3. Trinary Hard Drive
    By nickname_changed in forum Tech Board
    Replies: 14
    Last Post: 05-13-2005, 10:01 AM
  4. Problems with memory allocation in classes
    By MathFan in forum C++ Programming
    Replies: 6
    Last Post: 01-09-2005, 02:05 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM