Static Members String

2

I'm trying to create a static member in a class so I do not need to instantiate the class to get the value of the class.

In the examples I found on the internet, it references members int .

In my case I want the static member to be a string .

If I do the following:

class algumacoisa
{

public:
 algumacoisa();
~algumacoisa();

string texto;
static string recebetexto;

};

texto = "alguma frase aqui...";
string algumacoisa::recebetexto = texto;

The following error message appears:

  

error: qualified-id in declaration before '=' token

It is just to illustrate that if you assign a normal string variable to a static string variable, the described error occurs. The code is not complete, but you can get an idea of what it refers to.

    
asked by anonymous 22.10.2015 / 23:00

1 answer

2

Examples made anyway can have all sorts of problems. I did it right and it does not have any problems.

#include <iostream>
using namespace std;

class AlgumaClasse {

public:
    static string recebetexto;
};

string AlgumaClasse::recebetexto = "alguma frase aqui...";

int main() {
    cout << AlgumaClasse::recebetexto;
    return 0;
}

See running on ideone .

    
30.10.2015 / 16:03