CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Feb 2007
    Posts
    149

    structs verses classes? When to use them?

    Hi

    When is the best time to use structs over classes ?

    Or should one always use classes for object oriented programming ?
    Is structs such a relic of procedural programming ?


    I notice it doesn't seem like the concept exists in java. In java everything appears to just be a class - unless I'm mistaken.
    stephen

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: structs verses classes? When to use them?

    [see clarification below...]
    In C++ the ONLY difference between a class and a struct is the default visibility after the opening brace:

    Code:
    struct myStruct
    {
    // Items placed right here are public
    }
    
    class myClass
    {
    // Items placed right here are private
    }
    Anything beyond this is simply a matter of style, and consistancy matter most.
    Last edited by TheCPUWizard; May 23rd, 2007 at 01:52 PM.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Aug 2002
    Posts
    879

    Re: structs verses classes? When to use them?

    Last edited by blueday54555; May 24th, 2007 at 02:46 AM.

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

    Re: structs verses classes? When to use them?

    Any links to C# material just causes more confusion.

    In C++, there is no difference between a struct and a class except for what CPUWizard mentioned in his post.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: structs verses classes? When to use them?

    Agreeing with Paul, this links are more likely to cause confusion than to help. The C# [and other managed languages] rules are completely different. And there it DOES matter. But that is for another forum...

    <blueday54555> would you consider going back and editing your post, so as not to confuse potential readers?
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

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

    Re: structs verses classes? When to use them?

    Don't forget default inheritance:
    Quote Originally Posted by TheCPUWizard
    In C++ the ONLY difference between a class and a struct is the default visibility after the opening brace:

    Code:
    struct myStruct : myBase  // inherited publically
    {
    // Items placed right here are public
    }
    
    class myClass : myBase // inherited privately
    {
    // Items placed right here are private
    }
    Anything beyond this is simply a matter of style, and consistancy matter most.
    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. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: structs verses classes? When to use them?

    Quote Originally Posted by Graham
    Don't forget default inheritance:
    Graham is correct, the difference in default visibility effects BOTH locations.....

    1) The base class (if any) visibility
    2) Member visibility directly after the opening brace.

    [I tend to forget the first, since I tend to always explicitly declare base class visibility. ]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

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

    Re: structs verses classes? When to use them?

    Well, another one but not actually in relation to structures or classes but just the keywords - in specifying template type parameters, you cannot use struct in place of class (you can use typename, though).

  9. #9
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: structs verses classes? When to use them?

    In addition to the good answers so far. As for the style, you might consider using structures for simple structure-like objects, possibly without very many functions or inheritance or virtual functions, whereby classes could be used for extended objects with several or many functions, lots of data, possible inheritance, etc.

    It is hard to define a single all-encompasing style in this matter, but the following samples might be indicative of a kind of style.

    Sincerely, Chris.

    Code:
    struct SimpleDataAddressPair
    {
      unsigned int addr;
      unsigned char data;
      
      SimpleDataAddressPair(unsigned int a = 0u, unsigned char d = 0u) : addr(a), data(d) { }
    };
    
    class ExtensiveObject
    {
    private:
    
      int m1;
      int m2;
      int m3;
      int m4;
    
    public:
    
      ExtensiveObject() : m1(0), m2(0), m3(0), m4(0) { }
      virtual ~ExtensiveObject() { }
      
    public:
    
      virtual void do_this(void) { }
      virtual void do_that(void) { }
      virtual void do_the_other_thing(void) { }
    };
    Last edited by dude_1967; May 24th, 2007 at 01:27 AM. Reason: corrections...
    You're gonna go blind staring into that box all day.

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

    Re: structs verses classes? When to use them?

    dude_1967, you added a constructor to the POD struct - that is not necessary - POD structs can be initialized using the initializer list and don't necessarily need a constructor unless there in a non-POD type included - which would itself make the struct non-POD and it is best (style) to write them as classes.

  11. #11
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: structs verses classes? When to use them?

    Quote Originally Posted by exterminator
    dude_1967, you added a constructor to the POD struct - that is not necessary - POD structs can be initialized using the initializer list and don't necessarily need a constructor...
    Oh. Thank you for pointing that out, exterminator. Somewhere along the lines I seem to have forgotten about the initializer list.

    Sincerely, Chris.
    You're gonna go blind staring into that box all day.

  12. #12
    Join Date
    Apr 2007
    Posts
    21

    Re: structs verses classes? When to use them?

    @exterminator

    What is meant by initializer list. Will you give an example for it, will it be like this ?

    Code:
    struct SimpleDataAddressPair var = { 65, 'A' };
    Thanks

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

    Re: structs verses classes? When to use them?

    Quote Originally Posted by irs_b
    @exterminator

    What is meant by initializer list. Will you give an example for it, will it be like this ?

    Code:
    struct SimpleDataAddressPair var = { 65, 'A' };
    Yes.

  14. #14
    Join Date
    Nov 2004
    Posts
    13

    Re: structs verses classes? When to use them?

    Function object always can be implemented as a struct.
    For example:

    struct Op
    {
    bool operator()(const Foo& foo1, const Foo& foo2) const;
    };

    Because there's always no variables in a function object.

  15. #15
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: structs verses classes? When to use them?

    Quote Originally Posted by sinall
    Function object always can be implemented as a struct.
    For example:

    struct Op
    {
    bool operator()(const Foo& foo1, const Foo& foo2) const;
    };

    Because there's always no variables in a function object.
    I fail to see why this is any type of "special case"...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Page 1 of 2 12 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