0

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...?

KidWithAComputer
  • 311
  • 1
  • 2
  • 10

3 Answers3

9

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.

Mike Seymour
  • 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
  • @ChristianHackl: lol sounds about right ;p you too – Lightness Races in Orbit Mar 16 '15 at 17:18
6

The only difference between classes and structs 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.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
1

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).

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185