Defining a constant in a class

J

Joe Van Dyk

class Foo
{
private:
const std::string type_("FOO_TYPE");
};

I'd like to do something like that, but I don't think that's allowed.
Do I need to put type_ inside each of Foo's ctor initialization list?
 
I

Ian Collins

Joe said:
class Foo
{
private:
const std::string type_("FOO_TYPE");
};

I'd like to do something like that, but I don't think that's allowed. Do
I need to put type_ inside each of Foo's ctor initialization list?

You can use a static member, or in this case it might be easier to use a
member function:

std::string type() const { return "FOO_TYPE"; }
 
C

Chris ( Val )

Joe said:
class Foo
{
private:
const std::string type_("FOO_TYPE");
};

I'd like to do something like that, but I don't think that's allowed.
Do I need to put type_ inside each of Foo's ctor initialization list?

What's wrong with initialising a constant in a default constructor,
or even an overloaded one as follows:?

class Foo
{
private:
const std::string type_;
public:
Foo( const std::string& foo_type ) : type_( foo_type ) {}
};

int main()
{
Foo F( "FOO_TYPE" );

// ...

Cheers,
Chris Val
 
J

Joe Van Dyk

Ian said:
You can use a static member, or in this case it might be easier to use a
member function:

std::string type() const { return "FOO_TYPE"; }

Hm... in what situation would I want to use your first suggestion (and
in what situation would I want to use your second suggestion)?

Thanks,
Joe
 
A

Alf P. Steinbach

* Joe Van Dyk:
Hm... in what situation would I want to use your first suggestion (and
in what situation would I want to use your second suggestion)?

Using a static const data member is more work if you have to provide the
value in a header file and don't want it duplicated in every compilation
unit -- and if you do that (correctly, that is) then others may not
understand the code. ;-)

Also, a member function can be virtual, a static data member can't.

Thirdly, with a static data member of class type you run the risk of
initialization order fiasco, that it may be used before being initialized.
 
S

Salt_Peter

context please?
am I right?
for you constructor is not the default constructor.

He did not give you a default ctor.
If he had it would have looked like so:

class Foo
{
const std::string type_;
public:
Foo() : type_("FOO_TYPE") { }
Foo( const std::string& foo_type ) : type_( foo_type ) { }
};
 
J

Joe Van Dyk

Chris said:
What's wrong with initialising a constant in a default constructor,
or even an overloaded one as follows:?

class Foo
{
private:
const std::string type_;
public:
Foo( const std::string& foo_type ) : type_( foo_type ) {}
};

int main()
{
Foo F( "FOO_TYPE" );

// ...

Cheers,
Chris Val

It would have to be repeated in each ctor then, right? And I think
duplication is the root of some evil.
 
V

Victor Bazarov

Joe said:
It would have to be repeated in each ctor then, right? And I think
duplication is the root of some evil.

If the string doesn't change depending on the constructor (or its args)
then why is the member non-static? And if it is, in fact, static by
nature, declare it such, and initialise in one place, no duplication of
code, and more, no duplication of data.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top