1

I have a class

class C
{
  private:
    const static std::regex r;
}

and I want to give the variable r some string to parse, as example:

//ATTENTION: IT'S NOT CORRECT, HOW TO DEFINED IT CORRECTLY?
std::regex r("Hello\n");

and I have a function in the same class, where I have multiple if-statements to check if the string is parsed

if(std::regex_match(user_input,r)
{
  std::cout << "match";
}
else
{
  std::cout << "no match";
}

How can I use r correctly and compare user_input with r, when r has not been defined?

1 Answers1

0

Something like this:

// === header file c.h ===
#ifndef C_H
#define C_H
#include <regex>
class C
{
private:
   const static std::regex r;
};
#endif

// === source file c.cpp ===
#include "c.h"
const std::regex C::r{"Hello\n"};
Bo R
  • 2,334
  • 1
  • 9
  • 17
  • why is it only const and not const static? – malicization Dec 30 '20 at 21:18
  • Because there is only "one" instance of `r` for static members so the `static` keyword used in the class will not mean the same thing when defining the varaible in the cpp-file. There it would only mean to hide it from other translation units. – Bo R Dec 30 '20 at 21:43