Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error when using register storage class as class member variable

Tags:

c++

#include <iostream>
using namespace std;

class test
{
    public:
        register int a;
};

int main() {
    // your code goes here
    test t;
    t.a = 10;
    return 0;
}

I am getting following error :

error: storage class specified for 'a'

Is there any way to use register storage class as member variable ?

like image 832
Undefined Behaviour Avatar asked Mar 16 '26 05:03

Undefined Behaviour


1 Answers

According to storage duration:

The register specifier is only allowed for objects declared at block scope and in function parameter lists.

So you can't use it for class member variables.

Note, that this specifier is obsolete: it is deprecated since C++11 and totally removed since C++17 - just let the compiler do the optimizations.

like image 71
Edgar Rokjān Avatar answered Mar 25 '26 08:03

Edgar Rokjān



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!