CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 4 of 5 FirstFirst 12345 LastLast
Results 46 to 60 of 64

Thread: Struct vs Class

  1. #46
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: Struct vs Class

    Quote Originally Posted by manish_velankani
    Anything other than the default constructors uses additional bytes and cycles. Calling functions and methods cost bytes and cycles and possibly cache gets full. When using c++ on embedded systems this becoms an important factor.
    Do u mean to say speed comes into picture?????

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

    Re: Struct vs Class

    Quote Originally Posted by manish_velankani
    the memory allocation also differs in both
    There is *no*, *none*, *zero* difference between a "struct" and a "class" except for the access specifiers.

    Regards,

    Paul McKenzie

  3. #48
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Smile Re: Struct vs Class

    Quote Originally Posted by Paul McKenzie
    There is *no*, *none*, *zero* difference between a "struct" and a "class" except for the access specifiers.

    Regards,

    Paul McKenzie
    Thanks!

  4. #49
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: Struct vs Class

    Mr McKenzie,

    i was hoping u could answer my post #41

    Thanks in advance

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

    Re: Struct vs Class

    I think one reason for keeping struct is for compatibility with C.

  6. #51
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Struct vs Class

    Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types.
    This is completely untrue for C++. In fact, the whole quotation does not apply to C++.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #52
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Struct vs Class

    Quote Originally Posted by Pink Panther
    So why the distinction????? Why the need of a struct at all?????? They could have eliminated it totally from the C++ library, why keep it?????
    This could be be better asked the other way round:

    So why the distinction????? Why the need of a class at all?????? They could have eliminated it totally from the C++, why keep it?????

    For the earlier u got the common phrase as an answer "C- compatibility". U could use the code written in C, better say structs written in C from C++.

    What's the answer for this question ? Why the need of an extra keyword "class"?

    Cheers,
    Exterminator

  8. #53
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Struct vs Class

    You can ask Bjorne Stroustrup why he didn't simply have struct and not bother with class.

    Yes, you could write all your code using struct if you really wanted.

    You are also allowed to do this in templates:

    Code:
    template < typename T >
    or
    Code:
    template < class T >
    (I don't know if template< struct T > would work, I've never tried it).

    Anyway, the above are equivalent. Though I prefer to use typename in that situation.

  9. #54
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Struct vs Class

    structs and classes are "externally" exactly the same except for the default access specifiers.
    Internally there is absolutely no reason to implement differently classes and structs, but compilers are allowed by the ANSI standard to do what they want internally.
    A compiler can add big loops that do nothing in all class constructors and not add these big loops in struct constructors... That is legal, but of course it is really stupid, and it is absolutely sure that no compilers do that.

    So, you must consider that classes and structs are the same, and if the compiler make a difference, than it is not your responsibility.

    In fact, i see a point where a compiler may really treat differently structs and classes (but, i am sure that there is no compiler which acts like that):
    The compiler may modify the declaration order of classes fields to have a better alignment (grouping 4-bytes fields at the start of the class, then 2-bytes fields, then 1-byte field).
    And not modify this order for structs, to ensure that Win32 API is always correctly called, or any library written with another compiler.

  10. #55
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Struct vs Class

    Quote Originally Posted by exterminator
    So why the distinction????? Why the need of a class at all?????? They could have
    The default access specifier is not a negligeable thing.
    Forgetting to specify the access specifier is less dangerous with 'class' than with 'struct'.

    Morevoer, the word 'class' conceptually better describes what it is.

    And what follows is absolutely not official but is probable:
    Many compilers are conforming to the ANSI (or are very near to be fully ANSI compliant).
    But, also ensure a larger compatibility with old C compilers or C++ compilers (for example the "implementation" of structs is the same in many compilers).
    And, adding a new 'class' keyword allow compilers to internally implement classes with more freedom, and restrict this freedom with structs to ensure compatibility for code that uses structs instead of classes.
    So, if what i say is real, structs can only be slower or as fast as classes (because the compiler may take more freedom with classes than with structs, even if it is only a very implicit convention).

    I already heard that the struct keyword is only here to ensure compatibility with C code and that C++ code should not use at all this keyword (at least if it does not interact with C modules).
    Personally i use structs for very simple objects that cannot change, like the context parameter passed to the callback of enumerators, but maybe i should not do that.

  11. #56
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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 .
    Well...no...C structures are different from C++ structures. In both languages a structure is a collection of variables referenced under one name to keep related information together.
    Code:
    struct Structure
    {
      int Integer;
      float Float;
    };
    This code is valid for C and C++. But the similarity stops right here. To declare a variable of type 'Structure' you would do the following:
    Code:
    // C
    struct Structure structInfo;
    
    // C++
    Structure structInfo;
    As you can see they are different. That's because in C you are NOT defining a new data type with the definition of a structure. Therefore you need to add the keyword 'struct' in front of the name of the structure when you declare variables of it.

    In C++, however, a new type is defined when a structure is defined, and you can use this new type to declare variables, and the struct keyword is NOT needed.

    C++ structures are derived from C structures, therefore any C structure is also a valid C++ structure . However, C++ structures have some additional characteristics which C structures don't have. While C structures can contain variables only, a C++ structure can also contain functions. The following example would be a valid C++ structure but an invalid C one:
    Code:
    struct Structure
    {
      int GetValue();
      void SetValue(int Value);
    
    private:
      int Value;
    };

  12. #57
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Struct vs Class

    Quote Originally Posted by SuperKoko
    And, adding a new 'class' keyword allow compilers to internally implement classes with more freedom, and restrict this freedom with structs to ensure compatibility for code that uses structs instead of classes.
    I don't see how they can. Structs can do anything that classes can do: specifically, structs can contain member functions and they can inherit and be derived from.
    Code:
     class foo
     {
     public:
     	int bar;
     	int wibble;
     };
    is identical to:
    Code:
     struct foo
     {
     	int bar;
     	int wibble;
     };
    How can a compiler know your intentions well enough to treat them differently? How can it know that you won't use class foo in a situation where a C function might reference it as a struct? The only thing it can do is to make a decision based on the actual definition of the type, not on whether it happens to use class or struct as the introduction.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  13. #58
    Join Date
    Jul 2009
    Posts
    1

    Re: Struct vs Class

    Your professor is absolutely right: Structs are VALUE types and Classes are Reference Types and yes, there's a HUGE performance penalty if, for instance, you try to sort an array of objects containing structs because the whole array will be copied (value type), the sort will be performed and then returned to the calling method.

    I absolutely don't understand why would some create a structure bigger than 16 bytes or so... It also has to do whether you have to allocate memory from the heap or the stack... etc... I don't want to go any further, but definitely stick to that professor. He seems to know the subject well.

    Regards.

  14. #59
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Struct vs Class

    Your resurrect a 4 year old thread, just to ridicule yourself by babbling nonsense about a topic which you don't understand? I call that screwed up...
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  15. #60
    Join Date
    Aug 2007
    Posts
    858

    Re: Struct vs Class

    Quote Originally Posted by icaruus View Post
    Your professor is absolutely right: Structs are VALUE types and Classes are Reference Types and yes, there's a HUGE performance penalty if, for instance, you try to sort an array of objects containing structs because the whole array will be copied (value type), the sort will be performed and then returned to the calling method.
    This is 100% incorrect in C++ (as has already been said about a dozen times in this thread).

Page 4 of 5 FirstFirst 12345 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