views:

434

answers:

9

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

+13  A: 

In C++

struct {  };

is equivalent to

class { public: };

I.e. the members and attributes of a struct/class are by default public/private.

maxschlepzig
Inheritance is also public.
GMan
+3  A: 

struct and class are pretty much interchangeable - just with different defaults in that classes default to private inheritance and members, structs to public. The class keyword (and not struct) must be used for eg. "template <class T>".

That said, many programmers use the two to give a slight suggestion to a programmer reading the code: by using a struct you're subtly suggesting a less encapsulating, OO design. A struct might be used internal to a library - where getting at the guts of it all is fair game, whereas classes are used on the boundary where API changes would inconvenience clients and better abstraction is useful. This very loose convention has grown out of the difference in default accessibility - lazy/efficient/concise (take your pick) programmers do what's easiest unless there's a benefit otherwise, and not typing access specifiers is nice when possible.

Tony
+5  A: 

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.

Michael Anderson
+29  A: 

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

In short, yes, struct can inherit from class in C++.

Vite Falcon
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>?
augustin
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.
DeadMG
@DeadMG: Thanks. That's clear. I had looked for duplicates and didn't find them. I'm still glad I asked. Thanks.
augustin
You can even forward declare a struct as a class, and vice versa, but Visual C++ chokes on this under some conditions.
Fabio Fracassi
I still use structs for pure data, and classes when member functions are needed, even though they are the same. Force of habit.
Alexander Rafferty
+4  A: 

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 http://stackoverflow.com/questions/577465/in-c-can-i-derive-a-class-from-a-struct.

MBennett
+3  A: 

Yes. Struct can inherit from a class and vice versa. The accessibility rule is

$11.2/2- "In the 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."

EDIT 2: So you can change your class as:. Note that it is a bad idea to have public data members usually.

class user_messages {  // i also changed the name when OP was modified :)
public:   
   std::list<std::string> messages;  
};
Chubsdad
+3  A: 

Yes a struct can inherit from a class. struct and class differ only in the access-specifier assumed for the members and for a base classes (or structs) if not specified explicitly in C++ . For structs it's public. For classes it's private.

The sentence you quote from the manual is about the concept of a class in C++, as compared to the concept of a data structure in C. In C++ new keyword - class was introduced to better reflect the change in the concept, but for compatibility with code in C, an old keyword struct was left and it's meaning is as described above.

Maciej Hehl
+3  A: 

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.

DeadMG
Thanks for the additional legacy/historical comment.
augustin
+1  A: 

In C++, a struct is a class. An union is a class too. If a non-union class can derive from another non-union classes, structs can derive from other non-struct/non-union classes, and non-union/non-struct classes can derive from structs.

Johannes Schaub - litb