C++: Can a struct inherit from a class?

41,710

Solution 1

Yes, struct can inherit from class in C++.

In C++, classes and struct are the same except for their default behaviour with regards to inheritance and access levels of members.

C++ class

  • Default Inheritance = private
  • Default Access Level for Member Variables and Functions = private

C++ struct

  • Default Inheritance = public
  • Default Access Level for Member Variables and Functions = public

Solution 2

In C++

struct A { /* some fields/methods ... */ };

is equivalent to:

class A { public: /* some fields/methods ... */ };

And

class A { /* some fields/methods ... */ };

is equivalent to:

struct A { private: /* some fields/methods ... */ };

That means that the members of a struct/class are by default public/private.

Using struct also changes the default inheritance to public, i.e.

struct A { }; // or: class A { };
class B : A { };

is equivalent to

struct A { }; // or: class  A { };
struct B : private A { };

And the other way around, this

struct A { }; // or: class A { };
struct B : A { };

is equivalent to:

struct A { }; // or: class A { };
class B : public A { };

Summary: Yes, a struct can inherit from a class. The difference between the class and struct keywords is just a change in the default private/public specifiers.

Solution 3

The only difference between a struct and a class is the default access level for members (private for classes, public for structs). This means that a struct should be able to inherit from a class, and vice-versa.

However, there is usually a difference in how structs and classes are used that is not mandated by the standard. structs are often used for pure data, ( or objects without polymorphism depending on your projects preference) and classes are used for the other cases. I emphasise that this is just a stylistic difference and not required.

Solution 4

The main thing to understand is that structs come from C, whereas classes are C++. This means that although structs ARE first-class object-orientated citizens, they also have a legacy purpose, which is the reason for classes being separate and structs being default-access public. However, once this is done with, they're absolutely and totally identical and interchangable in every way.

Solution 5

A struct is the same thing as a class except that a class defaults its members to private while a struct defaults its members to public. As a result, yes, you can inherit between the two. See in C++, can I derive a class from a struct.

Share:
41,710
augustin
Author by

augustin

I am augustin.

Updated on October 20, 2020

Comments

  • augustin
    augustin over 3 years

    I am looking at the implementation of an API that I am using.

    I noticed that a struct is inheriting from a class and I paused to ponder on it...

    First, I didn't see in the C++ manual I studied with that a struct could inherit from another struct:

    struct A {};
    struct B : public A {};
    

    I guess that in such a case, struct B inherits from all the data in stuct A. Can we declare public/private members in a struct?

    But I noticed this:

     class A {};
     struct B : public A {};  
    

    From my online C++ manual:

    A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.

    Is the above inheritance valid even if class A has some member functions? What happen to the functions when a struct inherit them? And what about the reverse: a class inheriting from a struct?

    Practically speaking, I have this:

    struct user_messages {
      std::list<std::string> messages;
    };
    

    And I used to iterate over it like this foreach message in user_messages.messages.

    If I want to add member functions to my struct, can I change its declaration and "promote" it to a class, add functions, and still iterate over my user_messages.messages as I did before?

    Obviously, I am still a newbie and I am still unclear how structs and classes interact with each other, what's the practical difference between the two, and what the inheritance rules are...

  • augustin
    augustin over 13 years
    Thanks. That's clear. Does this mean that a struct can have member functions as well, including a constructor, and that we can create a new instance like for a class: <code>myObject = new myStruct(myVar)<code>?
  • Puppy
    Puppy over 13 years
    Yes. Anything that's valid for a class is valid for a struct. They're absolutely and totally identical, except the access modifiers given above. There are no limits on mixing them or anything.
  • augustin
    augustin over 13 years
    @DeadMG: Thanks. That's clear. I had looked for duplicates and didn't find them. I'm still glad I asked. Thanks.
  • Fabio Fracassi
    Fabio Fracassi over 13 years
    You can even forward declare a struct as a class, and vice versa, but Visual C++ chokes on this under some conditions.
  • Alexander Rafferty
    Alexander Rafferty over 13 years
    I still use structs for pure data, and classes when member functions are needed, even though they are the same. Force of habit.
  • nathanesau
    nathanesau about 8 years
    This should really be the chosen answer! Very clear.
  • Eljay
    Eljay over 6 years
    The inheritance accessibility examples are incorrect. struct A{}; class B : A{}; is equivalent to class A{}; class B : private A {};. The accessibility of the base is determined by the derived being class or struct, not determined by the base being class or struct.
  • maxschlepzig
    maxschlepzig over 6 years
    @Eljay, you are right, I had the examples mixed up. I've just updated my answer.