<p>In what scenarios is it better to use a <code>struct</code> vs a <code>class</code> in C++?</p>

[{'answer_id': 54596, 'author': 'Commodore Jaeger', 'author_id': 4659, 'author_profile': 'https://Stackoverflow.com/users/4659', 'pm_score': 11, 'selected': True, 'text': '<p>The differences between a <code>class</code> and a <code>struct</code> in C++ are:</p>\n<ul>\n<li><code>struct</code> members and base classes/structs are <code>public</code> by default.</li>\n<li><code>class</code>  members and base classes/structs are <code>private</code> by default.</li>\n</ul>\n<p>Both classes and structs can have a mixture of <code>public</code>, <code>protected</code> and <code>private</code> members, can use inheritance, and can have member functions.</p>\n<p>I would recommend you:</p>\n<ul>\n<li>use <code>struct</code> for plain-old-data structures without any class-like features;</li>\n<li>use <code>class</code> when you make use of features such as <code>private</code> or <code>protected</code> members, non-default constructors and operators, etc.</li>\n</ul>\n'}, {'answer_id': 54597, 'author': 'Andy', 'author_id': 3857, 'author_profile': 'https://Stackoverflow.com/users/3857', 'pm_score': 4, 'selected': False, 'text': "<p>For C++, there really isn't much of a difference between structs and classes. The main functional difference is that members of a struct are public by default, while they are private by default in classes. Otherwise, as far as the language is concerned, they are equivalent.</p>\n\n<p>That said, I tend to use structs in C++ like I do in C#, similar to what Brian has said. Structs are simple data containers, while classes are used for objects that need to act on the data in addition to just holding on to it.</p>\n"}, {'answer_id': 54599, 'author': 'enigmatic', 'author_id': 443575, 'author_profile': 'https://Stackoverflow.com/users/443575', 'pm_score': 3, 'selected': False, 'text': '<p>They are pretty much the same thing.  Thanks to the magic of C++, a struct can hold functions, use inheritance, created using "new" and so on  just like a class </p>\n\n<p>The only functional difference is that a class begins with private access rights, while a struct begins with public.  This is the maintain backwards compatibility with C.</p>\n\n<p>In practice, I\'ve always used structs as data holders and classes as objects.</p>\n'}, {'answer_id': 54628, 'author': 'mbyrne215', 'author_id': 5241, 'author_profile': 'https://Stackoverflow.com/users/5241', 'pm_score': 5, 'selected': False, 'text': '<p>One place where a struct has been helpful for me is when I have a system that\'s receiving fixed format messages (over say, a serial port) from another system.  You can cast the stream of bytes into a struct that defines your fields, and then easily access the fields.</p>\n\n<pre><code>typedef struct\n{\n    int messageId;\n    int messageCounter;\n    int messageData;\n} tMessageType;\n\nvoid processMessage(unsigned char *rawMessage)\n{\n    tMessageType *messageFields = (tMessageType *)rawMessage;\n    printf("MessageId is %d\\n", messageFields-&gt;messageId);\n}\n</code></pre>\n\n<p>Obviously, this is the same thing you would do in C, but I find that the overhead of having to decode the message into a class is usually not worth it.</p>\n'}, {'answer_id': 54656, 'author': 'Alan Hinchcliffe', 'author_id': 5436, 'author_profile': 'https://Stackoverflow.com/users/5436', 'pm_score': 4, 'selected': False, 'text': "<p>To answer my own question (shamelessly), As already mentioned, access privileges are the only difference between them in C++.  </p>\n\n<p>I tend to use a struct for data-storage only.  I'll allow it to get a few helper functions if it makes working with the data easier.  However as soon as the data requires flow control (i.e. getters/setters that maintain or protect an internal state) or starts acquring any major functionality (basically more object-like), it will get 'upgraded' to a class to better communicate intent.</p>\n"}, {'answer_id': 54830, 'author': 'argv0', 'author_id': 5595, 'author_profile': 'https://Stackoverflow.com/users/5595', 'pm_score': 4, 'selected': False, 'text': '<p>Structs (<a href="http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html" rel="noreferrer">PODs</a>, more generally) are handy when you\'re providing a C-compatible interface with a C++ implementation, since they\'re portable across language borders and linker formats.</p>\n\n<p>If that\'s not a concern to you, then I suppose the use of the "struct" instead of "class" is a good communicator of intent (as @ZeroSignal said above).  Structs also have more predictable copying semantics, so they\'re useful for data you intend to write to external media or send across the wire.  </p>\n\n<p>Structs are also handy for various metaprogramming tasks, like traits templates that just expose a bunch of dependent typedefs:</p>\n\n<pre><code>template &lt;typename T&gt; struct type_traits {\n  typedef T type;\n  typedef T::iterator_type iterator_type;\n  ...\n};\n</code></pre>\n\n<p>...But that\'s really just taking advantage of struct\'s default protection level being public...</p>\n'}, {'answer_id': 54870, 'author': 'Baltimark', 'author_id': 1179, 'author_profile': 'https://Stackoverflow.com/users/1179', 'pm_score': -1, 'selected': False, 'text': '<p>I never use "struct" in C++. </p>\n\n<p>I can\'t ever imagine a scenario where you would use a struct when you want private members, unless you\'re willfully trying to be confusing. </p>\n\n<p>It seems that using structs is more of a syntactic indication of how the data will be used, but I\'d rather just make a class and try to make that explicit in the name of the class, or through comments. </p>\n\n<p>E.g.</p>\n\n<pre><code>class PublicInputData {\n    //data members\n };\n</code></pre>\n'}, {'answer_id': 54902, 'author': 'Ferruccio', 'author_id': 4086, 'author_profile': 'https://Stackoverflow.com/users/4086', 'pm_score': 6, 'selected': False, 'text': '<p>The only time I use a struct instead of a class is when declaring a functor right before using it in a function call and want to minimize syntax for the sake of clarity. e.g.:</p>\n\n<pre><code>struct Compare { bool operator() { ... } };\nstd::sort(collection.begin(), collection.end(), Compare()); \n</code></pre>\n'}, {'answer_id': 1191852, 'author': 'Javier', 'author_id': 11649, 'author_profile': 'https://Stackoverflow.com/users/11649', 'pm_score': 2, 'selected': False, 'text': "<p>they're the same thing with different defaults (private by default for <code>class</code>, and public by default for <code>struct</code>), so in theory they're totally interchangeable.</p>\n\n<p>so, if I just want to package some info to move around, I use a struct, even if i put a few methods there (but not many).  If it's a mostly-opaque thing, where the main use would be via methods, and not directly to the data members, i use a full class.</p>\n"}, {'answer_id': 1191854, 'author': 'anio', 'author_id': 35227, 'author_profile': 'https://Stackoverflow.com/users/35227', 'pm_score': 2, 'selected': False, 'text': '<p>Structs by default have public access and classes by default have private access.</p>\n\n<p>Personally I use structs for Data Transfer Objects or as Value Objects. When used as such I declare all members as const to prevent modification by other code.</p>\n'}, {'answer_id': 1191880, 'author': 'quark', 'author_id': 29057, 'author_profile': 'https://Stackoverflow.com/users/29057', 'pm_score': 8, 'selected': False, 'text': '<p>As everyone else notes there are really only two actual language differences:</p>\n\n<ul>\n<li><code>struct</code> defaults to public access and <code>class</code> defaults to private access.</li>\n<li>When inheriting, <code>struct</code> defaults to <code>public</code> inheritance and <code>class</code> defaults to <code>private</code> inheritance.  (Ironically, as with so many things in C++, the default is backwards: <code>public</code> inheritance is by far the more common choice, but people rarely declare <code>struct</code>s just to save on typing the "<code>public</code>" keyword.</li>\n</ul>\n\n<p>But the real difference in practice is between a <code>class</code>/<code>struct</code> that declares a constructor/destructor and one that doesn\'t.  There are certain guarantees to a "plain-old-data" POD type, that no longer apply once you take over the class\'s construction.  To keep this distinction clear, many people deliberately only use <code>struct</code>s for POD types, and, if they are going to add any methods at all, use <code>class</code>es.  The difference between the two fragments below is otherwise meaningless:</p>\n\n<pre><code>class X\n{\n  public:\n\n  // ...\n};\n\nstruct X\n{\n  // ...\n};\n</code></pre>\n\n<p>(Incidentally, here\'s a thread with some good explanations about what "POD type" actually means: <a href="https://stackoverflow.com/questions/146452/what-are-pod-types-in-c">What are POD types in C++?</a>)</p>\n'}, {'answer_id': 1191890, 'author': 'Tal Pressman', 'author_id': 74060, 'author_profile': 'https://Stackoverflow.com/users/74060', 'pm_score': 5, 'selected': False, 'text': '<p>From the <a href="http://www.faqs.org/faqs/C++-faq/part4/" rel="noreferrer">C++ FAQ Lite</a>:</p>\n<blockquote>\n<p>The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults.</p>\n<p>struct and class are otherwise functionally equivalent.</p>\n<p>OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that\'s the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword.</p>\n</blockquote>\n'}, {'answer_id': 1191891, 'author': 'GaiusSensei', 'author_id': 139284, 'author_profile': 'https://Stackoverflow.com/users/139284', 'pm_score': -1, 'selected': False, 'text': '<p>I thought that Structs was intended as a Data Structure (like a multi-data type array of information) and classes was inteded for Code Packaging (like collections of subroutines &amp; functions)..</p>\n\n<p>:(</p>\n'}, {'answer_id': 1191939, 'author': 'harik', 'author_id': 136750, 'author_profile': 'https://Stackoverflow.com/users/136750', 'pm_score': 0, 'selected': False, 'text': '<p>I use struct only when I need to hold some data without any member functions associated to it (to operate on the member data) and to access the data variables directly.</p>\n\n<p>Eg: Reading/Writing data from files and socket streams etc. Passing function arguments in a structure where the function arguments are too many and function syntax looks too lengthy.</p>\n\n<p>Technically there is no big difference between class and struture except default accessibility.\nMore over it depends on programming style how you use it.</p>\n'}, {'answer_id': 1191966, 'author': 'Maciek', 'author_id': 142168, 'author_profile': 'https://Stackoverflow.com/users/142168', 'pm_score': 1, 'selected': False, 'text': '<p>Technically both are the same in C++ - for instance it\'s possible for a struct to have overloaded operators etc.</p>\n\n<p>However :</p>\n\n<p>I use structs when I wish to pass information of multiple types simultaneously\nI use classes when the I\'m dealing with a "functional" object.</p>\n\n<p>Hope it helps.</p>\n\n<pre><code>#include &lt;string&gt;\n#include &lt;map&gt;\nusing namespace std;\n\nstruct student\n{\n    int age;\n    string name;\n    map&lt;string, int&gt; grades\n};\n\nclass ClassRoom\n{\n    typedef map&lt;string, student&gt; student_map;\n  public :\n    student getStudentByName(string name) const \n    { student_map::const_iterator m_it = students.find(name); return m_it-&gt;second; }\n  private :\n    student_map students;\n};\n</code></pre>\n\n<p>For instance, I\'m returning a struct student in the get...() methods over here - enjoy.</p>\n'}, {'answer_id': 1195156, 'author': 'ogoid', 'author_id': 1608293, 'author_profile': 'https://Stackoverflow.com/users/1608293', 'pm_score': 4, 'selected': False, 'text': "<p>As every one says, the only real difference is the default access. But I particularly use struct when I don't want any sort of encapsulation with a simple data class, even if I implement some helper methods. For instance, when I need something like this:</p>\n\n<pre><code>struct myvec {\n    int x;\n    int y;\n    int z;\n\n    int length() {return x+y+z;}\n};\n</code></pre>\n"}, {'answer_id': 1196597, 'author': 'Khaled Alshaya', 'author_id': 127893, 'author_profile': 'https://Stackoverflow.com/users/127893', 'pm_score': 1, 'selected': False, 'text': "<blockquote>\n  <p>When would you choose to use struct\n  and when to use class in C++?</p>\n</blockquote>\n\n<p>I use <code>struct</code> when I define <code>functors</code> and <code>POD</code>. Otherwise I use <code>class</code>.</p>\n\n<pre><code>// '()' is public by default!\nstruct mycompare : public std::binary_function&lt;int, int, bool&gt;\n{\n    bool operator()(int first, int second)\n    { return first &lt; second; }\n};\n\nclass mycompare : public std::binary_function&lt;int, int, bool&gt;\n{\npublic:\n    bool operator()(int first, int second)\n    { return first &lt; second; }\n};\n</code></pre>\n"}, {'answer_id': 1628357, 'author': 'Adisak', 'author_id': 14904, 'author_profile': 'https://Stackoverflow.com/users/14904', 'pm_score': 5, 'selected': False, 'text': '<p>You can use "struct" in C++ if you are writing a library whose internals are C++ but the API can be called by either C or C++ code.  You simply make a single header that contains structs and global API functions that you expose to both C and C++ code as this:</p>\n\n<pre><code>// C access Header to a C++ library\n#ifdef __cpp\nextern "C" {\n#endif\n\n// Put your C struct\'s here\nstruct foo\n{\n    ...\n};\n// NOTE: the typedef is used because C does not automatically generate\n// a typedef with the same name as a struct like C++.\ntypedef struct foo foo;\n\n// Put your C API functions here\nvoid bar(foo *fun);\n\n#ifdef __cpp\n}\n#endif\n</code></pre>\n\n<p>Then you can write a function bar() in a C++ file using C++ code and make it callable from C and the two worlds can share data through the declared struct\'s.  There are other caveats of course when mixing C and C++ but this is a simplified example.</p>\n'}, {'answer_id': 36917400, 'author': 'Lightness Races in Orbit', 'author_id': 560648, 'author_profile': 'https://Stackoverflow.com/users/560648', 'pm_score': 8, 'selected': False, 'text': '<p>There are lots of misconceptions in the existing answers.</p>\n\n<p><a href="https://stackoverflow.com/a/34108140/560648"><strong>Both <code>class</code> and <code>struct</code> declare a class.</strong></a></p>\n\n<p>Yes, you may have to rearrange your access modifying keywords inside the class definition, depending on which keyword you used to declare the class.</p>\n\n<p>But, beyond syntax, the <em>only</em> reason to choose one over the other is convention/style/preference.</p>\n\n<p>Some people like to stick with the <code>struct</code> keyword for classes without member functions, because the resulting definition "looks like" a simple structure from C.</p>\n\n<p>Similarly, some people like to use the <code>class</code> keyword for classes with member functions and <code>private</code> data, because it says "class" on it and therefore looks like examples from their favourite book on object-oriented programming.</p>\n\n<p>The reality is that this completely up to you and your team, and it\'ll make literally no difference whatsoever to your program.</p>\n\n<p>The following two classes are absolutely equivalent in every way except their name:</p>\n\n<pre><code>struct Foo\n{\n   int x;\n};\n\nclass Bar\n{\npublic:\n   int x;\n};\n</code></pre>\n\n<p>You can even switch keywords when redeclaring:</p>\n\n<pre><code>class Foo;\nstruct Bar;\n</code></pre>\n\n<p>(although <a href="https://stackoverflow.com/q/33483623/560648">this breaks Visual Studio builds</a> due to non-conformance, so that compiler will emit a warning when you do this.)</p>\n\n<p>and the following expressions both evaluate to true:</p>\n\n<pre><code>std::is_class&lt;Foo&gt;::value\nstd::is_class&lt;Bar&gt;::value\n</code></pre>\n\n<p>Do note, though, that you can\'t switch the keywords when <em>redefining</em>; this is only because (per the one-definition rule) duplicate class definitions across translation units must <em>"consist of the same sequence of tokens"</em>. This means you can\'t even exchange <code>const int member;</code> with <code>int const member;</code>, and has nothing to do with the semantics of <code>class</code> or <code>struct</code>.</p>\n'}, {'answer_id': 39507590, 'author': 'ivan.ukr', 'author_id': 1540501, 'author_profile': 'https://Stackoverflow.com/users/1540501', 'pm_score': 1, 'selected': False, 'text': '<p>I use structs when I need to create POD type or functor.</p>\n'}, {'answer_id': 47168608, 'author': 'farhan', 'author_id': 5832798, 'author_profile': 'https://Stackoverflow.com/users/5832798', 'pm_score': 2, 'selected': False, 'text': "<h3>Class.</h3>\n\n<p>Class members are private by default.</p>\n\n<pre><code>class test_one {\n    int main_one();\n};\n</code></pre>\n\n<p>Is equivalent to</p>\n\n<pre><code>class test_one {\n  private:\n    int main_one();\n};\n</code></pre>\n\n<p>So if you try</p>\n\n<pre><code>int two = one.main_one();\n</code></pre>\n\n<p>We will get an error: <code>main_one is private</code> because its not accessible. We can\nsolve it by initializing it by specifying its a public ie</p>\n\n<pre><code>class test_one {\n  public:\n    int main_one();\n};\n</code></pre>\n\n<h3>Struct.</h3>\n\n<p>A struct is a class where members are public by default.</p>\n\n<pre><code>struct test_one {\n    int main_one;\n};\n</code></pre>\n\n<p>Means <code>main_one</code> is private ie</p>\n\n<pre><code>class test_one {\n  public:\n    int main_one;\n};\n</code></pre>\n\n<p>I use structs for data structures where the members can take any value, it's\neasier that way.</p>\n"}, {'answer_id': 47295230, 'author': 'Vorac', 'author_id': 1145760, 'author_profile': 'https://Stackoverflow.com/users/1145760', 'pm_score': 3, 'selected': False, 'text': "<p>An advantage of <code>struct</code> over <code>class</code> is that it save one line of code, if adhering to &quot;first public members, then private&quot;. In this light, I find the keyword <code>class</code> useless.</p>\n<p>Here is another reason for using only <code>struct</code> and never <code>class</code>. Some code style guidelines for C++ suggest using small letters for function macros, the rationale being that when the macro is converted to an inline function, the name shouldn't need to be changed. Same here. You have your nice C-style struct and one day, you find out you need to add a constructor, or some convenience method. Do you change it to a <code>class</code>? Everywhere?</p>\n<p>Distinguishing between <code>struct</code>s and <code>class</code>es is just too much hassle getting into the way of doing what we should be doing - programming. Like so many of C++'s problems it arises out of the strong desire for backwards compatibility.</p>\n"}, {'answer_id': 52366333, 'author': 'pasbi', 'author_id': 4248972, 'author_profile': 'https://Stackoverflow.com/users/4248972', 'pm_score': 3, 'selected': False, 'text': '<p>As others have pointed out</p>\n\n<ul>\n<li>both are equivalent apart from default visibility</li>\n<li>there may be reasons to be forced to use the one or the other for whatever reason</li>\n</ul>\n\n<p>There\'s a clear recommendation about when to use which from Stroustrup/Sutter:</p>\n\n<blockquote>\n  <p><a href="http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c2-use-class-if-the-class-has-an-invariant-use-struct-if-the-data-members-can-vary-independently" rel="noreferrer">Use class if the class has an invariant; use struct if the data members can vary independently</a></p>\n</blockquote>\n\n<p>However, keep in mind that it is not wise to forward declare sth. as a class (<code>class X;</code>) and define it as struct (<code>struct X { ... }</code>).\nIt may work on some linkers (e.g., g++) and may fail on others (e.g., MSVC), so you will find yourself in developer hell.</p>\n'}, {'answer_id': 52821071, 'author': 'Helping Bean', 'author_id': 9682036, 'author_profile': 'https://Stackoverflow.com/users/9682036', 'pm_score': 1, 'selected': False, 'text': "<p>All class members are private by default and all struct members are public by default. \nClass has default private bases and Struct has default public bases. Struct in case of C cannot have member functions where as in case of C++ we can have member functions being added to the struct. Other than these differences, I don't find anything surprising about them.</p>\n"}, {'answer_id': 58945793, 'author': 'Richard Chambers', 'author_id': 1466970, 'author_profile': 'https://Stackoverflow.com/users/1466970', 'pm_score': 3, 'selected': False, 'text': "<p>Both <code>struct</code> and <code>class</code> are the same under the hood though with different defaults as to visibility, <code>struct</code> default is public and <code>class</code> default is private. You can change either one to be the other with the appropriate use of <code>private</code> and <code>public</code>. They both allow inheritance, methods, constructors, destructors, and all the rest of the goodies of an object oriented language.</p>\n\n<p>However one huge difference between the two is that <code>struct</code> as a keyword is supported in C whereas <code>class</code> is not. This means that one can use a <code>struct</code> in an include file that can be <code>#include</code> into either C++ or C so long as the <code>struct</code> is a plain C style <code>struct</code> and everything else in the include file is compatible with C, i.e. no C++ specific keywords such as <code>private</code>, <code>public</code>, no methods, no inheritance, etc. etc. etc.</p>\n\n<p>A C style <code>struct</code> can be used with other interfaces which support using C style <code>struct</code> to carry data back and forth over the interface.</p>\n\n<p>A C style <code>struct</code> is a kind of template (not a C++ template but rather a pattern or stencil) that describes the layout of a memory area. Over the years interfaces usable from C and with C plug-ins (here's looking at you Java and Python and Visual Basic) have been created some of which work with C style <code>struct</code>.</p>\n"}, {'answer_id': 65944568, 'author': 'Tony Delroy', 'author_id': 410767, 'author_profile': 'https://Stackoverflow.com/users/410767', 'pm_score': 2, 'selected': False, 'text': '<p>Just to address this from a C++20 Standardese perspective (working from <a href="https://isocpp.org/files/papers/N4860.pdf" rel="nofollow noreferrer">N4860</a>)...</p>\n<p>A <em>class</em> is a type.  The keywords &quot;<code>class</code>&quot; and &quot;<code>struct</code>&quot; (and &quot;<code>union</code>&quot;) are - in the C++ grammar - <em>class-key</em>s, and the only functional significance of the choice of <code>class</code> or <code>struct</code> is:</p>\n<blockquote>\n<p>The <em>class-key</em> determines whether ... access is public or private by default (11.9).</p>\n</blockquote>\n<h2>Data member default accessibility</h2>\n<p>That the <code>class</code> keyword results in private-by-default members, and `struct keyword results in public-by-default members, is documented by the examples in 11.9.1:</p>\n<blockquote>\n<p>class X {\nint a;    // X::a is private by default: class used</p>\n</blockquote>\n<p>...vs...</p>\n<blockquote>\n<p>struct S {\nint a;    // S::a is public by default: struct used</p>\n</blockquote>\n<h2>Base class default accessibility</h2>\n<p>1.9 also says:</p>\n<blockquote>\n<p>In the absence of an <em>access-specifier</em> for a base class, <code>public</code> is assumed when the derived class is defined with the <em>class-key</em> <code>struct</code> and <code>private</code> is assumed when the class is defined with the <em>class-key</em> <code>class</code>.</p>\n</blockquote>\n<h2>Circumstances where consistent use of struct or class is required...</h2>\n<p>There\'s a requirement:</p>\n<blockquote>\n<p>In a redeclaration, partial specialization, explicit specialization or explicit instantiation of a class template, the <em>class-key</em> shall agree in kind with the original class template declaration (9.2.8.3).</p>\n</blockquote>\n<blockquote>\n<p>...in any <em>elaborated-type-specifier</em>, the <code>enum</code> keyword shall be used to refer to an enumeration (9.7.1), the <code>union</code> <em>class-key</em> shall be used to refer to a <code>union</code> (11.5), and either the <code>class</code> or <code>struct</code> <em>class-key</em> shall be\nused to refer to a non-union class (11.1).</p>\n</blockquote>\n<p>The following example (of when consistency is <em>not</em> required) is provided:</p>\n<blockquote>\n<p>struct S { } s;\nclass S* p = &amp;s;       // OK</p>\n</blockquote>\n<p>Still, some compilers may warn about this.</p>\n<hr/>\n<p>Interestingly, while the types you create with <code>struct</code>, <code>class</code> and <code>union</code> are all termed &quot;classes&quot;, we have...</p>\n<blockquote>\n<p>A <em>standard-layout struct</em> is a standard layout class defined with the <em>class-key</em> <code>struct</code> or the <em>class-key</em> <code>class</code>.</p>\n</blockquote>\n<p>...so in Standardese, when there\'s talk of a <em>standard-layout struct</em> it\'s using &quot;struct&quot; to imply &quot;not a union&quot;s.</p>\n<p>I\'m curious if there are similar use of &quot;struct&quot; in other terminology, but it\'s too big a job to do an exhaustive search of the Standard.  Comments about that welcome.</p>\n'}, {'answer_id': 69608567, 'author': 'user13947194', 'author_id': 13947194, 'author_profile': 'https://Stackoverflow.com/users/13947194', 'pm_score': 2, 'selected': False, 'text': "<p>After years of programming in C++, my main language, I come to the dead conclusion that this is another one of C++ dumb feature.</p>\n<p>There is no real difference between the two, and no reason why I should spend extra time deciding whether I should define my entity as a struct or a class.</p>\n<p>To answer this question, feel free to always define your entity as a struct. Members will be public by default which is the norm. But even more importantly, inheritance will be public by default. Protected inheritance, and even worse, private inheritance, are the exceptions.</p>\n<p>I have never had a case where private inheritance was the right thing to do. Yes I tried to invent problems to use private inheritance but it didn't work. And Java, the role model of Object Oriented programming defaults to public inheritance if you don't use the accessor keywords. And by the way, Java doesn't allow accessor keywords on inherited classes, they can only be publicly inherited. So you can see, the cpp team really fell down here.</p>\n<p>Another frustrating thing about this, is that if you define as a class and declare as a struct you get compilation warning. As though this is something that impacted the performance or accuracy of your program. One answer also noted that MSVC may propogate a compiler error instead.</p>\n<p>Those persons that use classes when it is raining and structs when it is shining are doing so based on what they have been taught. It's not something they discovered to be true. Java does not have a pair of names for classes, and only have the class keyword. If you want a data structure, simply make all your members public and don't add functions. This works in Java and I don't see any problem. What's the problem? You need 4 or 5 characters of BOM code to determine how to interpret the context of a class entity.</p>\n"}, {'answer_id': 73944553, 'author': 'Jyotirmoy', 'author_id': 1690511, 'author_profile': 'https://Stackoverflow.com/users/1690511', 'pm_score': -1, 'selected': False, 'text': '<p>If your program demands object oriented approach you can go for class, or else struct will meet most of your needs.</p>\n'}]
['https://Stackoverflow.com/questions/54585', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5436/']