Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can unnamed structures inherit?

The following looks like a compilation error :

struct : Base { };

Yet when used [1] it seems to work :

#include <iostream>
using namespace std;

template<bool B>
struct A 
{
    struct : std::integral_constant<bool, B> {
    } members;
};

int main()
{
    A<true> a;    
    cout << a.members.value << endl;
    return 0;
}

In c++ is it valid for unnamed structures to inherit? Are there any examples where this is userful?


[1] Disclaimer: I'm not pretending the provided example is useful. I rarely use unnamed structs, and when I do they're usually bundling together some built-in member variables, in order to provide a cleaner interface for a class. The question came up from the observation that memberspaces need not be nammed structures

like image 483
Nikos Athanasiou Avatar asked Jun 01 '14 12:06

Nikos Athanasiou


People also ask

Can structure be inherited?

A struct cannot inherit from another kind of struct, whereas classes can build on other classes. You can change the type of an object at runtime using typecasting. Structs cannot have inheritance, so have only one type. If you point two variables at the same struct, they have their own independent copy of the data.

Which member function Cannot be inherited?

Constructor cannot be inherited but a derived class can call the constructor of the base class.

Can you inherit from a struct C?

No you cannot. C does not support the concept of inheritance.

How is inheritance implemented in C?

You can easily implement single inheritance in C by literally embedding the inherited class attribute structure as the first member of the derived class attribute structure. With this arrangement, you can always safely pass a pointer to Rectangle to any C function that expects a pointer to Shape.


2 Answers

Unnamed classes can inherit. This is useful, for example, in situations when you must inherit in order to override a virtual function, but you never need more than one instance of the class, and you do not need to reference the derived type, because a reference to the base type is sufficient.

Here is an example:

#include <iostream>
using namespace std;

struct Base {virtual int process(int a, int b) = 0;};
static struct : Base {
    int process(int a, int b) { return a+b;}    
} add;
static struct : Base {
    int process(int a, int b) { return a-b;}    
} subtract;
static struct : Base {
    int process(int a, int b) { return a*b;}    
} multiply;
static struct : Base {
    int process(int a, int b) { return a/b;}    
} divide;

void perform(Base& op, int a, int b) {
    cout << "input: " << a << ", " << b << "; output: " << op.process(a, b) << endl;
}

int main() {
    perform(add, 2, 3);
    perform(subtract, 6, 1);
    perform(multiply, 6, 7);
    perform(divide, 72, 8);
    return 0;
}

This code creates four anonymous derivations of Base - one for each operation. When the instances of these derivations are passed to the perform function, the proper override is called. Note that perform does not need to know about any of the specific types - the base type with its virtual function is enough to complete the process.

Here is the output of running the above code:

input: 2, 3; output: 5
input: 6, 1; output: 5
input: 6, 7; output: 42
input: 72, 8; output: 9

Demo on ideone.

like image 129
Sergey Kalinichenko Avatar answered Oct 18 '22 04:10

Sergey Kalinichenko


Your first example, because it doesn't declare anything, shows an attempt at an anonymous struct (which is not allowed - 7/3) rather than an unnamed one (which is).

The grammar in 9/1 of the C++11 standard seems to allow an unnamed class to have a base, so I think your second example is fine.

like image 26
Alan Stokes Avatar answered Oct 18 '22 02:10

Alan Stokes