Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 158685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:43:31+00:00 2026-05-11T10:43:31+00:00

My first post so please go easy on me! I know that there’s no

  • 0

My first post so please go easy on me!

I know that there’s no real difference between structs and classes in C++, but a lot of people including me use a struct or class to show intent – structs for grouping ‘plain old data’ and classes for encapsulated data that has meaningful operations.

Now, that’s fine but at what point do you start to think that something isn’t just a struct anymore and should become a class?

Things I think are reasonable for structs to have:

  1. constructors with simple initialisation code only.
  2. serialization code such as stream insertion / extraction operators.

Things I’m not so sure about, but would probably do:

  1. comparison operators
  2. Simple transformation functions – for example byteswapping all the members after receiving data from an external source.

I don’t think structs should have:

  1. dynamic memory allocation.
  2. destructor.
  3. complex member functions.

Where do the boundaries lie???

Also, is it reasonable to have class instances as members of a struct? e.g.

class C {private: int hiddenData; public: void DoSomething();};  struct S {int a; float b; C c; };  S s; s.c.DoSomething(); 

Remember, I’m not on about what you CAN do with C++, I’m interested in what you SHOULD do when designing good software.

Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:43:31+00:00Added an answer on May 11, 2026 at 10:43 am

    Class vs. struct

    Using class or struct keyword is a matter of taste together with the ‘feeling‘ it produces on the reader. Technically they are equivalent, but readability is better if structs are used for PODs and C-struct types and classes for anything else.

    Basic things that should go in a C++ struct: constructor that initializes the data (I dislike using memset, and it can later bite back if the POD evolves into something different) or construction from other types but not copy constructor.

    If you need to define a copy constructor or assignment operator because the compiler generated is not good enough, make it a class.

    It is common to use structs also for functors that will be passed to STL algorithms and template metaprogramming, as in

    struct square_int {    int operator()( int value )    {       return value*value;    } }; std::transform( v.begin(), v.end(), v.begin(), square_int() ); 

    or

    // off the top of my head template <typename T> struct is_pointer { enum { value = false } };  template <typename T> struct is_pointer<T*> { enum { value = true } }; 

    Member methods vs. free functions

    Besides what I have said before, that do not add to what others already answered, I wanted to put some focus on other types of functions that you comment in your post, as comparison operators and the like.

    Operators that are meant to be symmetric (comparison, arithmetic), insertion and deletion operators and transformations are usually better implemented as free functions regardless of whether you declare it as a class or struct.

    Symmetric operators (with regard to data types) are not symmetric if they are implemented as member functions. The lookup rules won’t cast the left hand side to call a member function, but it will apply the same cast to match a free function.

       // Example of symmetry with free functions where method would be asymmetric    int main()    {       std::string( 'Hello ' ) + 'world'; // compiles as free / member function       'Hello ' + std::string( 'world' ); // compiles with free function, fails with member function definition of +    } 

    In the code above, if operator+ were a member method of std::string the compiler would fail to compile as it cannot cast the const char* literal into a std::string to use the member method.

    Insertion and extraction from streams must always be implemented as free functions as the stream is always the left hand side of the operation.

    Keeping transformations as free functions decouple the two different types. If A and A’ can be converted into one another and you decide to implement transformations as members of A, then A must know A’ and all uses of A will depend on A’ whether you use it or not. If you define the transformation as a free function, A is complete without A’ and the coupling between the two classes/structs will be smaller. The same goes for transformations to/from network, serialization and deserialization. When you implement them inside the class/struct you are forcing all users to know about those transforms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My first post here, please be gentle. =) I work for a company that
First post, so here goes. I'm writing a script that does intelligent search and
First of all, I know how to build a Java application. But I have
First time posting a question on StackOverflow, so please go easy on me :)
This is my first post on Stack Overflow so please exuse (and feel free
For Starters, i would like to note, that this is my first post on
If Model->validates() returns false is there an easy way to know why? I am
This is my first post in any forum so please bear with me. I
This is my first post on stackoverflow.com so please be kind (rewind) ;) I
This is my first post. Please forgive me for asking a basic question as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.