Thread: copy struct to class

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    copy struct to class

    Can anyone tell me how I can copy the contents of a struct into a class so that the class's member functions can access the data contained within the struct?

    I need to be able to access a struct with the member functions of a seperate class... and I do not know how to do this.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Can't you just pass the struct as a pointer to a member function?
    Eg:
    Code:
    class someclass
    {
    public:
    readstruct(somestruct * astruct);
    
    };
    
    
    readstruct(somestruct * astruct)
    {
     
    return astruct->somedata;
    
    
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    Ya I assumed a pointer was involved, however i do not know how to do this... (im very very new to this)...

    I mean what is the :
    Code:
    return astruct->somedata;
    arrow for?

    and I need to be able to copy the data from the struct into something that the class can access...

    Not to mention I get errors:

    :22: `mystruct' was not declared in this scope
    22: `b_struct' was not declared in this scope
    22: invalid data member initialization
    22: (use `=' to initialize static data members)
    22: ISO C++ forbids declaration of `readstruct' with no type


    When I add this to my class.h file:

    public:
    readstruct(mystruct * b_struct);
    Last edited by guda; 10-06-2003 at 08:27 PM.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I was just giving an example, there is no "somestuct" struct...or any value named "somedata" for that matter...
    Code:
    return astruct->somedata;
    the "->" operator is basically the same as:
    Code:
    return (*astruct).somedata;
    (or something like that, just use -> instead of . when working with ptrs)

    I would suggest finding a good pointer tutorial and reading up on them...I'll post one if i have time, but its getting late and I have homework to do
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM