By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,872 Members | 2,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,872 IT Pros & Developers. It's quick & easy.

Defining a constant in a class

P: n/a
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?
Apr 20 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Joe Van Dyk wrote:
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"; }

--
Ian Collins.
Apr 20 '06 #2

P: n/a

Joe Van Dyk wrote:
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

Apr 20 '06 #3

P: n/a
Ian Collins wrote:
Joe Van Dyk wrote:
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"; }


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
Apr 21 '06 #4

P: n/a
* Joe Van Dyk:
Ian Collins wrote:
Joe Van Dyk wrote:
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"; }


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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 21 '06 #5

P: n/a
am I right?
for you constructor is not the default constructor.
Apr 21 '06 #6

P: n/a
context please?

fcvcnet wrote:
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 ) { }
};
Apr 21 '06 #7

P: n/a
Chris ( Val ) wrote:
Joe Van Dyk wrote:
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


It would have to be repeated in each ctor then, right? And I think
duplication is the root of some evil.
Apr 21 '06 #8

P: n/a
Joe Van Dyk wrote:
Chris ( Val ) wrote:
Joe Van Dyk wrote:
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


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 21 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.