navig8tr
navig8tr

Reputation: 1844

Initialization after class declaration C++

In the following constructor I'd like to initialize the _register bitset along with POLY. Is it possible to initialize more than one bitset after the colon? Is there another way to initialize a bitset in a constructor?

private:
    std::string message;        
    const std::bitset<4> POLY; 
    std::bitset<4> _register;  

public:
    CRC4(std::string message); // constructor declared 

// constructor defined
CRC4::CRC4(std::string message) : POLY (std::string("0011")) // initialize POLY 
{
    this->message.assign(message); // initialize message 
}

Thanks for any suggestions.

Upvotes: 0

Views: 241

Answers (1)

Daniel Frey
Daniel Frey

Reputation: 56863

You can add any number of intializations to the initializer list, separated by comma:

CRC4::CRC4(std::string message)
  : message( message ), // initialize message
    POLY (std::string("0011")), // initialize POLY
    _register(std::string("0011")) 
{
}

Upvotes: 1

Related Questions