Member initialization while using delegated constructor
Posted
by
Anton
on Stack Overflow
See other posts from Stack Overflow
or by Anton
Published on 2012-08-30T04:46:02Z
Indexed on
2012/08/30
15:38 UTC
Read the original article
Hit count: 413
I've started trying out the C++11 standard and i found this question which describes how to call your ctor from another ctor in the same class to avoid having a init method or the like. Now i'm trying the same thing with code that looks like this:
hpp:
class Tokenizer
{
public:
Tokenizer();
Tokenizer(std::stringstream *lines);
virtual ~Tokenizer() {};
private:
std::stringstream *lines;
};
cpp:
Tokenizer::Tokenizer()
: expected('=')
{
}
Tokenizer::Tokenizer(std::stringstream *lines)
: Tokenizer(),
lines(lines)
{
}
But this is giving me the error:
In constructor ‘config::Tokenizer::Tokenizer(std::stringstream*)’:
/path/Tokenizer.cpp:14:20: error: mem-initializer for ‘config::Tokenizer::lines’ follows constructor delegation
I've tried moving the Tokenizer() part first and last in the list but that didn't help.
What's the reason behind this and how should i fix it? I've tried moving the lines(lines)
to the body with this->lines = lines;
instead and it works fine. But i would really like to be able to use the initializer list.
Thanks in advance!
© Stack Overflow or respective owner