CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 5 1234 ... LastLast
Results 1 to 15 of 64

Thread: Struct vs Class

  1. #1
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Struct vs Class

    So we all know the main difference between classes and structures:

    Classes default as private, structures default as public.

    But when it comes down to deciding whether or not to use classes or structures on a basis of performance, this small difference isn't enough.

    I would like to know all known differences between classes and structures. I was told by my professor that a structure directly accesses data whereas classes reference their data as a pointer kind of, making structures faster than classes. I don't know I think this is BS, but I want to know what you guys think.

    Thanks!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Struct vs Class

    Quote Originally Posted by MrDoomMaster
    So we all know the main difference between classes and structures:

    Classes default as private, structures default as public.

    But when it comes down to deciding whether or not to use classes or structures on a basis of performance, this small difference isn't enough.

    I would like to know all known differences between classes and structures.
    You just named one -- classes have the default access specifier as private, while for struct's the default specifier is public.

    One other that you didn't mention is that classes are inherited privately by default, while structs are inherited publicly.

    Otherwise, there is no difference.
    I was told by my professor that a structure directly accesses data whereas classes reference their data as a pointer kind of, making structures faster than classes.
    Get another professor -- that's pure gobbledy-gook.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: Struct vs Class

    I don't think that you can compare the two in any shape or form. They are two completely different things. However...

    Imo, it depends on your choice. I prefer using structures from time to time (yes, I know, it's a C thang ) when I have to make dynamic information container thinga-ma-bobs (linked lists, BSTs, etc.) Classes I use in order to gain a level of abstraction from stuff that I don't want to mess up (that's a major selling point of OOP!)
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Struct vs Class

    Quote Originally Posted by YourSurrogateGod
    I don't think that you can compare the two in any shape or form. They are two completely different things.
    No they are not "completely different". Except for the differences in the default access specifier, structs and classes are exactly the same thing.
    Code:
    struct  foo
    {
        private:
              int x;
        public:
             foo();
             virtual int func1();
    };
    Is the same as
    Code:
    class  foo
    {
        private:
              int x;
        public:
             foo();
             virtual int func1();
    };
    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Struct vs Class

    Some reasons for using structure in C++:

    1. working with a legacy program
    2. communicating a structure with traditional programming API (including networking)
    3. making a dumb object for database record

  6. #6
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: Struct vs Class

    Quote Originally Posted by Paul McKenzie
    No they are not "completely different". Except for the differences in the default access specifier, structs and classes are exactly the same thing.
    Code:
    struct  foo
    {
        private:
              int x;
        public:
             foo();
             virtual int func1();
    };
    Is the same as
    Code:
    class  foo
    {
        private:
              int x;
        public:
             foo();
             virtual int func1();
    };
    Regards,

    Paul McKenzie
    Wow, never seen that done before with a struct. Now if structs can do inheritance then the world will come to an end .
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  7. #7
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Struct vs Class

    Quote Originally Posted by YourSurrogateGod
    Now if structs can do inheritance then the world will come to an end .
    It does and the default is public inheritance. The world hasn't ended yet and life still goes on.
    Last edited by Kheun; March 14th, 2005 at 09:41 PM.

  8. #8
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: Struct vs Class

    Quote Originally Posted by Kheun
    It does and the default is public inheritance. The world hasn't ended yet and life still goes on.
    /me falls over...
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  9. #9
    Join Date
    May 2004
    Posts
    340

    Re: Struct vs Class

    there is no need to talk about it any more,it is just household. though there may be some difference when u apply it in the your programme,it is no longer a story
    regards,
    jolley

  10. #10
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: Struct vs Class

    Quote Originally Posted by jolley
    there is no need to talk about it any more,it is just household. though there may be some difference when u apply it in the your programme,it is no longer a story
    regards,
    jolley
    Well, to be frank, I wasn't really aware of the possibilities that struct had . I could have even simplified some of my C programs to a degree .
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  11. #11
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: Struct vs Class

    Quote Originally Posted by MrDoomMaster
    I was told by my professor that a structure directly accesses data whereas classes reference their data as a pointer kind of, making structures faster than classes. I don't know I think this is BS, but I want to know what you guys think.
    Thanks!
    I agree with Paul McKenzie's comments about getting another professor.
    However, there are many professors with very out dated programming knowledge.

    There are many teachers who teach C/C++ language who have never even seen the actual C/C++ standard.

    There is a common practice among some C++ programmers in which they use struct for POD types, and class for non-POD types.
    In GENERAL POD types are easy and fast to construct, and have no explicit constructors or destructors.

    However, the C++ standard itself has nothing to say about this common practice, and there's nothing to stop you from using a class to create a POD type, or using a struct to create a non-POD type.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  12. #12
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: Struct vs Class

    Quote Originally Posted by YourSurrogateGod
    Well, to be frank, I wasn't really aware of the possibilities that struct had . I could have even simplified some of my C programs to a degree .
    Not really.
    C programs do not allow struct to have member functions.
    Furthermore, you can not use a constructor to create a struct object in C.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  13. #13
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Struct vs Class

    Quote Originally Posted by Axter
    Not really.
    C programs do not allow struct to have member functions.
    Furthermore, you can not use a constructor to create a struct object in C.
    Well, there are not constructors and member functions in C at all.
    Last edited by ovidiucucu; March 15th, 2005 at 04:40 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  14. #14
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Struct vs Class

    Quote Originally Posted by Paul McKenzie
    Get another professor -- that's pure gobbledy-gook.
    Only if that was so easy... I would have done that with several professors when I was in college...
    Quote Originally Posted by god
    me falls over...
    Just sit down and take a very deep breath.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  15. #15
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Struct vs Class

    Quote Originally Posted by cilu
    Only if that was so easy... I would have done that with several professors when I was in college...
    Now I'm sorry I never accepted a professor after an interview, put him/her write/maintain "real-life" code to improve your college courses quality.
    Come on... they only taught you where to search... and I can presume this is one reason you found codeguru..
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Page 1 of 5 1234 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured