CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2009
    Posts
    12

    Stucts and classes

    I was just looking at some code on C++ for using delete and I noticed that the code for
    the struct was just like what you'd write for a class and it worked the same.

    Code:
    #include <iostream>
    #include <new>
    using namespace std;
    
    struct myclass {
      myclass() {
    	  cout <<"myclass constructed\n";
      }
      ~myclass() {
    	  cout <<"myclass destroyed\n";}
    };
    
    int main () {
      myclass * pt;
    
      pt = new myclass;
      delete pt;
    
      return 0;
    }
    I modified the above code slightly turning it into a class and got the same result.

    class myclass {

    public:

    myclass() {
    cout <<"myclass constructed\n";
    }
    ~myclass() {
    cout <<"myclass destroyed\n";}
    };

    int main () {
    myclass * pt;

    pt = new myclass;
    delete pt;

    return 0;
    }

    [/code]

    So what i was wondering what's the difference between classes and structs cos from what
    i can see they can both be used to make classes, any reason to use one over the other?

    Thanx.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Stucts and classes

    There are a couple of different default assumptions between the two (notice you had to put in the public: tag on the class but not the struct), but both can be used identically other than that.

    Some coding styles will prefer structs for pure data containers and classes for information-hiding objects, but there's nothing in the language which suggests that relationship; just tradition.

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: Stucts and classes

    If you're talking about C++ then the difference between a class and a struct becomes more washed out with each newer compiler. (The difference used to be quite vast in C, but not in C++ anymore.)

    Semantically structs should be used to represent light-weight custom types, and other more complex constructs should be implemented as classes. Some older C++ compilers might put a heavier memory imprint on the code if you "overload" the struct, or use class in a "lighter" code, but I'm sure that's not the issue with all the newer compilers.

    Another difference is how your IDE environment is set up to "read/recognize" your source code and how easy will it be for you to work with your data if you declare it as a strust instead of a class and vice versa. Some IDE environments use a certain "user-friendly" grouping/color coding for classes and separately for structs, and/or may allow automated adding/editing of methods and variables within classes and not structs.

    Lastly -- and I'm sure there's also a bunch of other subtleties besides what I listed here -- is that all member variables and methods not explicitly declared as public/private/protected in a struct become public, and private in a class.

    The bottom line is that if I were you I'd not attempt to reinvent the bicycle and use those two reserved words as they were designed.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Stucts and classes

    Quote Originally Posted by Bontakun View Post
    So what i was wondering what's the difference between classes and structs cos from what
    i can see they can both be used to make classes, any reason to use one over the other?
    The reason why struct and class are almost identical is given by Stroustrup in The Design and Evolution of C++.

    C style structs must be available in C++ because C is a subset of C++. But when Stroustrup introduced the class concept in C++ he didn't want a limited version of the same concept. That's why he made struct and class close to identical. Why he didn't make them totally identical I don't know. Maybe he'd had a beer too many or too few when he made the decision.

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