Thread: Access a struct elements from...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Question Access a struct elements from...

    Well, I decided to try some FAQ tutorials I hadent been able to understand before. Namely, linked lists and binary trees. I'm on linked lists at the moment, I read through it, and decided I wanted to make a wrapper class for my list, so I could acess list elements from within a class.

    But, I've never used structs... Ever. Only classes. So, I'm quite confused as to how to access a struct element from inside a wrapper class:

    Code:
    #define Next      4
    #define Prev       8
    #define Previous  8
     typedef struct element *Wha;
    
     struct Wha {
      Wha prev;
      Wha next;
      int clams;
     };
    
     class WhaWrapper {
      private:
       Wha Confusing;
    
      public:
       WhaWrapper();
       WhaWrapper(int I) {;} //I want to set the int clams from here in the Wha class.
     };
    Thanks for your help!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    If you know how to access members of a class, then its no different with a struct. Essentially, a class is exactly the same as a struct, with one difference; a struct defaults all members public, wheras a class defaults all members private. From a style point of view, most programmers only use a struct when there are no member functions, and all data members are public (mimic'ing the use of a struct in 'C').

    If you're creating a linked list, a node in a linked list must at least point to the node which is next and/or previous to itself. but at the moment, your data structure doesn't contain any pointers at all.
    I suggest you get to grips with a basic singly-linked list (the simplest kind of linked list, with only one pointer in each node) before creating any wrapper classes and other fancy stuff on top of that.
    Last edited by Bench82; 08-26-2006 at 04:55 AM.

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ah, I see. I misread, confused Element with element in the tutorial. Heh, I didint realize structs were simplfidied classes, thanks.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Blackroot
    Ah, I see. I misread, confused Element with element in the tutorial. Heh, I didint realize structs were simplfidied classes, thanks.
    Almost, except that structs are not simplified from classes in any way at all. everything that you can do with a class, can be done in a struct, and vice versa. the only difference is the default access rights. which is why use of class vs struct is purely a style issue. The reason why struct is commonly used for structures which contain only public data members is more to do with the historical connection to 'C'.
    Last edited by Bench82; 08-26-2006 at 05:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. How can I access a struct (from a header file)?
    By loxslay in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2006, 01:25 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM