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 9187949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:53:51+00:00 2026-06-17T19:53:51+00:00

The classes and structs have one difference between them (as far as I know),

  • 0

The classes and structs have one difference between them (as far as I know), that the struct defaults to public and class defaults to private. And then I came to know that there is a similar kind of data type which is also used in a similar manner, that is union. The union can not be used as a base class in inheritance (i don’t know what that means, but I still accept it).

I wanted to know whether there are some particular instances, where struct/ union/ class, or they can be used interchangeably (except for the cases I enlisted)? Please do tell me if I am wrong somewhere.
Regards

  • 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. Editorial Team
    Editorial Team
    2026-06-17T19:53:52+00:00Added an answer on June 17, 2026 at 7:53 pm

    My use of class, struct and union is the following:

    • class for objects that have behaviour.
    • struct for passive data.
    • union for very special cases where different data requires to be accessed as different types.

    I’ve read this (except the union point) in the Google C++ Style guide a long time ago and I was following it since then.

    Using structs to carry passive data (objects without behaviour attached to the object) have the advantage of default publicness of the members, so they can be accessed without Getters and Setters. If some member data needs to be checked/modified before assign or some member data needs to be computed/modified before be getted, IMHO they need a Setter/Getter pair and the object is a class instead of a struct.

    For the union type, I find it useful for some kind of data structures that requires some weird access to the members, or needs some members to be treated as another type in some contexts. For example a 3D vector or a IP address:

    union 3DVector
    {
        double x, y, z;
        double vector[3];
    } v;
    
    // Acess members with name
    v.x = 6.0; v.y = 7.0; v.z = 8.0;
    // Acess members as a vector
    Normalize(v.vector);
    
    union IPAddress
    {
        int binary;
        char octet[4];
    } ip;
    
    // Acess the binary address
    std::cout << std::hex << ip.binary << '\n';
    // Print in a human-readable form
    std::cout << static_cast<int>(ip.octet[0]) << '.'
              << static_cast<int>(ip.octet[1]) << '.'
              << static_cast<int>(ip.octet[2]) << '.' 
              << static_cast<int>(ip.octet[3]) << '\n';
    

    The above functionality could be achieved overloading operators and conversion operators, but the union approach looks neat for me.

    The unions can also be templated and can have constructor/destructor, this could be useful for serialization purposes (not for all kind of objects):

    template <typename T> union Serializer
    {
        Serializer(const T &o) : object(o) {}
        T object;
        char binary[sizeof(T)];
    };
    
    SomePODObject obj; // Only POD objects please!
    Serializer s(obj);
    SendBuffer(s.binary);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that one of the differences between classes and structs is that struct
I know that the difference between defines and constants is that constants have type,
I have many structs (classes) and standalone functions that I like to compile separately
I have 2 classes: one basic and one derivative. Also have structure. struct D;
Hello I'm having trouble figuring this out. I have these structs and classes. struct
A short, maybe stupid question. For classes and structs, sometimes I like to have
I have a problem. There are two classes: struct Base { Base* retain() {
I know it helps a lot if we structure our programs using classes, structs
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char
Possible Duplicate: What are the differences between struct and class in C++ This question

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.