I read that the main difference between a class and a structure is that class is reference type and structure is value type. can anybody explain me what does the value type and reference type means...?
-
Classes can have a constructor and destructor while structures cannot. – ForceBru Mar 16 '15 at 16:12
-
4I think you were reading about C#. – molbdnilo Mar 16 '15 at 16:14
-
2@ForceBru: A struct can have a constructor. http://stackoverflow.com/questions/1127396/struct-constructor-in-c – NathanOliver Mar 16 '15 at 16:15
-
4@ForceBru: Complete and utter nonsense. In C++, at least. – Christian Hackl Mar 16 '15 at 16:16
-
@ChristianHackl, oops, I'm more a C guy than C++ – ForceBru Mar 16 '15 at 16:22
-
1@ForceBru: Yet C does not have classes. So it's still nonsense. – Lightness Races in Orbit Mar 16 '15 at 16:38
-
@LightnessRacesinOrbit, I meant, I'm not a C++ expert, so I may be mistaken as I know C better as C++. And sure, I know C doesn't have classes! – ForceBru Mar 16 '15 at 16:40
3 Answers
You must be thinking of a different language. In C++, class types are semantically the same whether you introduce them with the class
or struct
keyword. They are object types (which one might loosely call "value types"), in the sense of being objects with a value representation.
The only difference is that base classes and members are public by default if you use struct
, and private if you use class
.
Reference types are denoted with &
or &&
, and can refer to any object or function type, not just classes.

- 249,747
- 28
- 448
- 644
-
I'm not sure if "semantically the same" is a good way to put it. There is clearly a "felt" difference between the two. Using `class` or `struct` is a piece of documentation which immediately gives you a strong hint on whether you are dealing with a loose collection of data or a complex entity with rules and invariants. – Christian Hackl Mar 16 '15 at 16:30
-
1@ChristianHackl: C++ defines semantics, not your heart. While I largely follow that convention, too, it's subjective and not at all close to being universally accepted. – Lightness Races in Orbit Mar 16 '15 at 16:39
-
1@ChristianHackl: "Semantically the same" is exactly the way to put it. They denote the same program behaviour (i.e. they have the same semantics), whether or not you read any extra meaning into the choice of keyword. – Mike Seymour Mar 16 '15 at 16:43
-
@LightnessRacesinOrbit: Of course it's subjective and not *universally* accepted. That's not the point. The point was that "semantically the same" may give the wrong impression that it does not matter at all to anyone which keyword to use, when "most developers" or "most people" (https://isocpp.org/wiki/faq/classes-and-objects) apparently do make a distinction. – Christian Hackl Mar 16 '15 at 16:54
-
1@ChristianHackl: It will only give the wrong impression if you don't know what "semantically" means. – Mike Seymour Mar 16 '15 at 16:59
-
1@ChristianHackl: Well, again, that's not relevant to _semantics_ :) – Lightness Races in Orbit Mar 16 '15 at 17:04
-
@MikeSeymour: Well, I had no intention of entering into a real argument about correct terminology. I'd personally just have used "semantics" as a synonym for "meaning" in a sentence like "[accessibility aside,] they are technically identical but differ in semantics to many programmers". Would you consider such a sentence outright wrong? – Christian Hackl Mar 16 '15 at 17:06
-
@LightnessRacesinOrbit: Hmm, I think I should reconsider my (programming-related) use of the word "semantics". I can see how it may easily cause me to say things I don't mean. – Christian Hackl Mar 16 '15 at 17:08
-
1@ChristianHackl: If you had no intention of entering into an argument about correct terminology, then beginning your feedback of this question with a criticism about terminology probably wasn't the smartest move ;) – Lightness Races in Orbit Mar 16 '15 at 17:10
-
@LightnessRacesinOrbit: No no, my intention was virtuous! (I even upvoted this answer.) I sought to eliminate a word which I thought had the potential of misinterpretation and causing fights about terminology :) Oh the irony. In fact, many, many years ago I had a strange internet flamewar in a CSS newsgroup when I was mentioning "CSS semantics" and everyone jumped at me and told me that "CSS has no semantics, only HTML is for semantics". Well, I digress. Have a nice evening! – Christian Hackl Mar 16 '15 at 17:16
-
The only difference between class
es and struct
s is that by default members/bases are private
to a class
but public
to a struct
.
Now values and references are totally orthogonal concepts in C++ to class
/struct
, basically meaning instance of a class
/struct
and handle-to-instance.

- 27,315
- 3
- 37
- 54
In c++, the only differences between a struct and a class is the default member access and default inheritance:
struct A : BaseClassOrStruct { // public inheritance
int member; // public member
}
class A : BaseClassOrStruct { // private inheritance
int member; // private member
}
However, I usually do make a distinction between them: I use a struct to indicate that my objects really are just a collection of data members (that typically have public access) without methods (other than setters and getters).

- 109,796
- 11
- 89
- 185
-
That's not the only difference. See the other answers and the duplicate. – juanchopanza Mar 16 '15 at 16:27
-
@juanchopanza If you think its better now, you could undo your downvote ;) – 463035818_is_not_an_ai Mar 17 '15 at 09:28