0

Is a C++ class without inhertitance, overloaded operators, contructors or destructor and private members same as a C struct?

My criteria for "same" are as follows:

  • They are interchangeable in the code
  • The machine code the compiler generate are same on a line by line basis
  • It makes sense to say they are the same (on a conversation)

If I were to slap a class and make every member public, do I still have the same program? Is the generated assembly same?

Essentially, is a C++ POD equivalent to a C struct with the same members?

Doga Oruc
  • 783
  • 3
  • 16
  • 3
    Essentially, there is very little/no difference between a `class` and a `struct` in C++, other than a `struct`'s members and bases are `public` by default, whereas a `class`'s members and bases are `private` by default. – Remy Lebeau Apr 27 '21 at 20:13
  • 1
    @RemyLebeau The question isn't about class vs struct, it's about POD vs. non-POD. – Barmar Apr 27 '21 at 20:15
  • 2
    @Barmar that is not how the question reads to me – Remy Lebeau Apr 27 '21 at 20:16
  • 3
    @RemyLebeau He said "C struct" not "C++ struct". He wants to know if C++ POD types are equivalent to a C struct with the same member declarations. – Barmar Apr 27 '21 at 20:18
  • No, C language does not support overload operator, constructors, destructors or accessibility types (private, protected, public). – Thomas Matthews Apr 27 '21 at 20:19
  • 5
    I don't understand why this question is closed. I'm not asking about what POD are, I'm asking if POD types are the same as C structs... – Doga Oruc Apr 27 '21 at 20:20
  • In C, when you do (e.g.): `struct mystruct { ... };`, `mystruct` goes into the "struct namespace". In C++, `mystruct` is _also_ defined as a _type_ (as if you used `typedef`). See: https://stackoverflow.com/questions/27212766/is-there-a-technical-reason-for-the-struct-namespace-in-c So, in C++, you could say: `struct mystruct { mystruct *next; };` In C, you'd have to say: `typedef struct mystruct mystruct; struct mystruct { mystruct *next; };` – Craig Estey Apr 27 '21 at 20:21
  • 2
    @DogaOruc you did not make that clear in your question, that is why it got closed. I suggest you [edit] your question to clarify what you are actually looking for. – Remy Lebeau Apr 27 '21 at 20:22
  • 2
    Agreed. I’ve reopened, but you should really clarify your criteria for “the same as”. – Sneftel Apr 27 '21 at 20:24
  • 1
    [Useful documentation.](https://en.cppreference.com/w/cpp/named_req/PODType) – user4581301 Apr 27 '21 at 20:31
  • on the dupes you will find a lot of information about the relation betwen C++ POD types and C structs. E.g.: "Roughly speaking, a POD type is a type that is compatible with C and perhaps equally importantly is compatible with certain ABI optimisations." and more on the many answers there. – bolov Apr 27 '21 at 20:31
  • I think we should re-open, so I can paste the information on the page @user4581301 gave as an answer. Other unfortunate souls may save some time by that answer. – Doga Oruc Apr 27 '21 at 20:34
  • 1
    The answer is yes, C++ POD types are layout-compatible with C struct. – rustyx Apr 27 '21 at 20:34
  • @DogaOruc I closed it because some of the answers to that question make it clear that they're equivalent. – Barmar Apr 27 '21 at 20:35
  • @DogaOruc no, we shouldn't reopened. Please don't start a reopen war. The duplicates contain all the information needed. – bolov Apr 27 '21 at 20:35
  • There are some differences I didn't see covered on that question, such as the C++ requirement that an object of a particular type must exist before it may be accessed, which has no counterpart in C, though I don't think there's ever been a consensus about what exactly corner cases should be supported in either language. – supercat Apr 27 '21 at 20:36
  • @bolov What do you mean? I'm only trying to help others who might ask the same question. I gain nothing from answering my own question. I also didn't find those dupes to be helpful, that is why I asked the question in the first place. – Doga Oruc Apr 27 '21 at 20:37
  • Note that the link I gave above has been superseded by C++20, so if you're dancing on the cutting edge your mileage may vary. – user4581301 Apr 27 '21 at 20:37
  • @bolov: C++ adds some restrictions on how structures may be used which aren't present in the C Standard, and which I didn't see addressed in that other question. – supercat Apr 27 '21 at 20:37
  • 2
    @supercat: But those differences don't seem relevant, as they are differences between C and C++, not between the types themselves. That is, whether two types are layout compatible in the different languages, the languages *themselves* are different and therefore you may need to write different code to utilize them properly. – Nicol Bolas Apr 27 '21 at 20:40
  • @DogaOruc it is helpful. People searching for this same question will find this question and then will go to the dupes and find the answer. – bolov Apr 27 '21 at 20:42
  • 1
    @supercat if you feel there is useful information not covered in the other answers, go to the most relevant dupe and add an answer there. – bolov Apr 27 '21 at 20:44
  • There may be edge case differences. For example, if the `struct` contains a `union` member variable, the rules differ between C and C++. Is that kind of difference important for *sameness*? – Eljay Apr 27 '21 at 20:54
  • @DogaOruc -- *Is a C++ POD equivalent to a C struct with the same members?* -- The term "POD" now means *Trivially Copyable* in the C++ standard. A trivially copyable type can have items in it that `C` has no idea of, such as `private` and `public` member variables, copy constructor, etc. Instead of POD, the current term to use if you are comparing C and C++ structs is "Standard Layout". So if you're asking whether a C++ type that has a standard layout is a C struct, the answer is yes. – PaulMcKenzie Apr 27 '21 at 21:11
  • Was `std::is_pod::value`, now is would be the combination of `std::is_standard_layout::value` and `std::is_trivial::value` (which is itself both `std::is_trivially_copyable::value`, and `std::is_trivially_default_constructible::value`). Or an array of such; possibly cv-qualified. – Eljay Apr 27 '21 at 22:00

0 Answers0