Thread: passing a struct to a class

  1. #1
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    passing a struct to a class

    I was wondering if it could be done... I thought I read somewhere that it could, but I don't remember... these were the only guidelines that I found:
    Containers can hold standard objects as well as custom objects, as long as the objects in the container meet a few requirements:
    • The object must have a default constructor,
    • an accessible destructor, and
    • an accessible assignment operator.
    if it can, how? what I'm trying to do is something like this:
    Code:
    /* MAIN.CPP */
    ...
    int main()
    {
       ...
       struct player
       {
           int atk,def,hp;
       };
    
       player p1
    
       loginClass login;
       login.init(p1);
       ...
    }
    the loginClass class is in a different file and gets linked... what I want it (login.init) to do is just open up a file and fill in the struct from that file... I don't really have any real code written yet, I just need to know how to write it...
    Last edited by major_small; 02-28-2004 at 06:31 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    look up function parameters and passing by reference.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I did... how do you think I got the guidelines... what I want to know is if you can pass a struct, and if so, how? do you have to globally define the struct, or is defining the struct in the same scope you created an instance of the class enough?

    the struct has to stay (at least) in main, because other classes will be using it...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Containers can hold standard objects as well as custom objects, as long as the objects in the container meet a few requirements:

    The reason for the above is simply so we can to manipulate our ADT's just like native ones:

    Code:
     struct object
    {
      virtual ~object() // MUST be virtual
     {
    
     }
      object() // default constructor
     {
      data = 0;
     }
      object(const object & x) // copy constructor
     {
      *this = x;     // see operator =, below 
     }
      const object & operator = (const object & x) 
     {
      data = x.data;
      return *this;  // so that we can chain assignments together 
     }
     
    public:  
     int data; 
    };
    Then you will be able to do:

    object a, b, c = a = b;


    >> do you have to globally define the struct, or is defining the struct in the same scope you created an instance of the class enough?


    It works just the same as with any other data type. You can make it global or better still, pass it as a parameter to the functions that will be manipulating it (but create it in main, of course, so that it doesn't get destroyed prematurally).


    The loading of your object might go something like:


    Code:
     bool loginClass::init(player & p)
    {
     bool success = false;
    
     ifstream in("player.dat");
    
         if(in.good())
        {
         in >> p.atk >> p.def >> p.hp;
           
             if(!in.fail())  success = true;
        }
     
     return success;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ^that won't complain that 'player' is undefined? (I don't have access to a compiler right now)

    thanks for the help, btw
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    the struct has to be made before the function. Doesnt have to be directly before it. For instance, you can create it in a .h file, and include that file into your main before you include the file with that loginClass.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM