What are the differences between struct and class in C++?

后端 未结 30 3053
盖世英雄少女心
盖世英雄少女心 2020-11-21 05:38

This question was already asked in the context of C#/.Net.

Now I\'d like to learn the differences between a struct and a class in C++. Please discuss the technical d

相关标签:
30条回答
  • 2020-11-21 06:08

    While implied by other answers, it's not explicitly mentioned - that structs are C compatible, depending on usage; classes are not.

    This means if you're writing a header that you want to be C compatible then you've no option other than struct (which in the C world can't have functions; but can have function pointers).

    0 讨论(0)
  • 2020-11-21 06:08

    **UPDATE: ** Please ignore this reply. I did not consider the possibility that for the struct, the value was uninitialized but just happened to be 0. There is not an initialization difference between struct and class.


    I am seeing another different between structs and classes having to do with default initialization.

    struct Foo {
        int a;
    };
    
    class Bar {
        int a;
    };
    
    class Tester {
        Foo m_Foo = Foo();
        Bar m_Bar = Bar();
    
    public:
        Tester() {}
    };
    
    int main() {
        auto myTester = Tester();
    }
    

    Run that code and examine myTester. You'll find that for m_Foo, the struct, m_Foo.a has been initialized to 0, but for m_Bar, the class, m_Bar.a is uninitialized. So there does appear to be a difference in what the default constructor does for struct vs. class. I'm seeing this with Visual Studio.

    0 讨论(0)
  • 2020-11-21 06:09

    The only other difference is the default inheritance of classes and structs, which, unsurprisingly, is private and public respectively.

    0 讨论(0)
  • 2020-11-21 06:09
    1. Member of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct (or union) are public by default.

    2. In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class.

    3. You can declare an enum class but not an enum struct.

    4. You can use template<class T> but not template<struct T>.

    Note also that the C++ standard allows you to forward-declare a type as a struct, and then use class when declaring the type and vice-versa. Also, std::is_class<Y>::value is true for Y being a struct and a class, but is false for an enum class.

    0 讨论(0)
  • 2020-11-21 06:10

    ISO IEC 14882-2003

    9 Classes

    §3

    A structure is a class defined with the class-key struct; its members and base classes (clause 10) are public by default (clause 11).

    0 讨论(0)
  • 2020-11-21 06:12

    1) Members of a class are private by default and members of struct are public by default.

    For example program 1 fails in compilation and program 2 works fine.

    // Program 1
    #include <stdio.h>
    
    class Test {
        int x; // x is private
    };
    int main()
    {
      Test t;
      t.x = 20; // compiler error because x is private
      getchar();
      return 0;
    }
    Run on IDE
    // Program 2
    #include <stdio.h>
    
    struct Test {
        int x; // x is public
    };
    int main()
    {
      Test t;
      t.x = 20; // works fine because x is public
      getchar();
      return 0;
    }
    

    2) When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private.

    For example program 3 fails in compilation and program 4 works fine.

    // Program 3
    #include <stdio.h>
    
    class Base {
    public:
        int x;
    };
    
    class Derived : Base { }; // is equilalent to class Derived : private Base {}
    
    int main()
    {
      Derived d;
      d.x = 20; // compiler error becuase inheritance is private
      getchar();
      return 0;
    }
    Run on IDE
    // Program 4
    #include <stdio.h>
    
    class Base {
    public:
        int x;
    };
    
    struct Derived : Base { }; // is equilalent to struct Derived : public Base {}
    
    int main()
    {
      Derived d;
      d.x = 20; // works fine becuase inheritance is public
      getchar();
      return 0;
    }
    
    0 讨论(0)
提交回复
热议问题