Is struct reference type or value type?

I heard that class in C++ is a reference type, and struct is a value type. So the performance of class is better than struct. What about in rust?

This is not true.

In Rust, the type system is very uniform, and as such, everything is a value. References and other pointer-like types are values. Functions are values. Enums are values, etc. Similarly, any value of any type can be put behind indirection, so you can have references to structs, Boxes of enums, references-to-references, and so on.

There is no automatic heap allocation or automatic indirection involved with structs or any other type, if that's what you are asking for.


By the way, there is no such thing as a "reference type" in C++, either, in the sense that languages like Java and C# use the expression. In C++, you can create classes by-value, without indirection, and without dynamic memory allocation, in the exact same way you would do with a struct. So whoever told you all this nonsense about "reference types" in C++ was wrong. The only difference between a C++ class and struct is default visibility (structs are public by default, whereas classes are either private or protected by default, I forgot exactly which one).

References in C++ are their own set of parametric types, just like pointers, arrays, functions, etc., so you can have a reference-to-class, but also a reference-to-struct or a reference-to-int or a reference-to-array, etc. They are explicit and appear in the type system, and they are basically just sugar for an immediately dereferenced pointer (so they preserve lvalue-ness). There isn't implicit referencing or automatic heap allocation in C++, either, and there is no intrinsic performance difference between classes and structs.

5 Likes

Consider that program:

class Foo {
 public:
  int x;
};

void print_foo(struct Foo foo) {
    std::cout << foo.x << std::endl;
}

int main() {
    struct Foo foo{42};
    print_foo(foo);
}

It's valid C++ program, but is type Foo value or reference? It's class in the beginning and struct in the end! How can that be?

The answer is simple: class and struct are one and the same. There are [almost] no difference. You may declare something as class and then use as struct (and the other way around, too).

What you have heard is this rule (very badly mangled): use a struct only for passive objects that carry data; everything else is a class.

This rule is there to save one line, essentially. Compare the program above to this:

struct Foo {
  int x;
};

void print_foo(class Foo foo) {
    std::cout << foo.x << std::endl;
}

int main() {
    class Foo foo{42};
    print_foo(foo);
}

It's one line shorter because struct members are public by default. That's it, there are no other difference.

That rule (structs for passive objects, class for active ones) is not a C++ rule at all. In fact style guide talks about that explicitly: The struct and class keywords behave almost identically in C++. We add our own semantic meanings to each keyword, so you should use the appropriate keyword for the data-type you're defining.

Rust doesn't have such two almost-but-not-quite interchangeable keywords thus this trick wouldn't work in Rust.

3 Likes