Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in C++, can I derive a class from a struct

The question says it all really. Am I allowed derive a class from a struct, or should I create a class that embeds my struct and defines copy constructors and an = operator to move between the two?

like image 328
SmacL Avatar asked Feb 23 '09 12:02

SmacL


People also ask

Can struct can be used as a base class for another class?

Struct cannot be a base class. So, Struct types cannot abstract and are always implicitly sealed. Abstract and sealed modifiers are not allowed and struct member cannot be protected or protected internals.

Can class inherit from struct C#?

Structs and inheritanceIt is not possible to inherit from a struct and a struct can't derive from any class. Similar to other types in . NET, struct is also derived from the class System.

Is struct in C same as class in C++?

The only difference between a struct and class in C++ is the default accessibility of member variables and methods. In a struct they are public; in a class they are private.

Is inheritance possible with structures?

Yes. The inheritance is public by default.


1 Answers

In C++ struct is (almost) synonymous to a class (except of different default access level), so yes, you can.

struct A { // fields are public by default };  class B: public A { // fields are private by default }; 

I'm not familiar with MFC, but it looks like an attempt to maintain both C and C++ APIs.

like image 89
Alex B Avatar answered Oct 05 '22 16:10

Alex B