Thread: Classes or Structs faster for Lists

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    19

    Classes or Structs faster for Lists

    Are stucts faster than classes for linked lists?

    I think stucts are easier to initialize becuase you dont need a constructor but its easier to have functions inside a class.

    Also its a bit simpler to point to a struct than a class.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    structs are just classes with all public data. Neither is faster than the either. I don't see how its easier to point to a struct than a class.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    106
    I thought structs were better since they came from the C language. Someone once told me that C is better than C++. Oh well, veteran oppinions could mis guide you sometimes

  4. #4
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    C is not better than C++. C++ is not better than C. They are different, and each has it's own strong points. You don't see many new games being made in C, and you don't see any operating systems written in C++.

    I think structs are easier to understand and write code with. I don't like classes too much, so I personally use structs for linked lists. Sometimes classes make things more complicated than they need to be.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    c++ structs can have all the advanced features of c++ classes. They a basicly the same.

  6. #6
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    But we use them differently.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    struct's and classes...mmm

    I see no real diff if you are using c++, other than (already mentioned) struct's default data to public and classes default them to private. In c++ both can contain method's. If Im wrong correct me.

    But personally it comes down to personal preference.


    kwigibo

  8. #8
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    I don't think classes default to private, but they do have the ability to store private data. Structs cannot hold private data.

  9. #9
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Actually classes are supposed to default to private access, and structs are and do default to public access.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  10. #10
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    Do this:

    - define a structure
    - declare an array of at least 2 elements
    - in the program print out the addesses of each element in each structure

    then:

    - define a class
    - declare an array with atleast 3 elements of the class
    - in the program print out the address of each element in each class

    From this, you should see that at run time the structure and the class are treated identically. The performance issues will be in function calls and how deep they are nested which you can control in the design.

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    19
    sigma
    Yes i see what you mean.

    I guess the main difference is that a class uses a constuctor making the declaration different unless you declare all of its members public so you have dirrect access to them.

    My big problem is finding a way to link a four dimensional list dynamically.

    Currently Im doing it directly which would become very difficult when I add more elements to the list.

  12. #12
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    Well here is some advice, STRUCTS and CLASS's are different, yes they are, if you are using structs you typically use strict C code. (in professional code you do)

    structs then cant have constructors or destructors, (HUGE Difference) and cannot have multipul inheritence.

    now thats makes structs and class's different.

    for linked lists tho, use structs, since its typically what is used in professional code.

    by the way class's are defaulted to private, and structs are defaulted to public.. also structs can have private data members.

    the guy who said they cant, do u code much?
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  13. #13
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I didn't know structs could have private data. I code a fair amount, you can check the CVS linked from my sig to see the code I've written.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  14. #14
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Yeah, but I'm just learning c++ and I'm a sophomore in high school. I guess my teacher is wrong...

  15. #15
    If you need a constructor, it would be better form to use a CLASS.

    As a matter of preference, if you are going to embed functions into your objects- use a CLASS - for straight data members it's okay to use a struct.

    How are u able to inherit from a STRUCT is something I've never heard of- inheritance and polymorphism are one of the major improvements of C++ over C.

    I've never heard of STRUCTS holding private data and don't think it is possible unless someone wishes to show me otherwise.

    Last thing, I don't think that CLASS's are defualted to private, they are defualted to public unless otherwise noted. This is becuase a constructor is public and if you were to create data member or member functions inside the CLASS without using specifications the access would be public. All functions and members would be accessible from outside the class.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs or classes?
    By legit in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2009, 10:16 AM
  2. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  3. Why use Classes instead of Structs?
    By yaya in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2008, 12:39 AM
  4. Are structs and classes the same thing?
    By dwks in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2005, 03:21 PM
  5. Classes or Structs?
    By DeanDemon in forum C++ Programming
    Replies: 12
    Last Post: 12-05-2002, 03:58 AM