473,396 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Defining a constant in a class

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
8 1835
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

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
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
* 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
am I right?
for you constructor is not the default constructor.
Apr 21 '06 #6
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jani Yusef | last post by:
I have a HW problem stated as shown at the top of the solution. The thing is is that I am not 100% sure wtf constant memory means. Well, I think I do but I am confused. Does my solution use contant...
8
by: Martin Magnusson | last post by:
I want to create a few objects which all share a lot of common features, so they should all derive from the same base class. However, each of them needs individual definitions of two functions. So...
38
by: Ted | last post by:
Is there a way to define a constant value for a color in a CSS declaration? I have numerous colors in different CSS elements, and if I make a change in color, I have to change all the reference...
29
by: Joe | last post by:
This is a very basic question, but why can't I do the following ? class schedule { private: static const string mScheduleFile = "schedule.txt"; .... }
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
4
by: Hans | last post by:
Hi, I want to define a couple of constant strings, like in C: #define mystring "This is my string" or using a const char construction. Is this really not possible in Python? Hans
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
6
by: Amit Bhatia | last post by:
Hi, I am not sure if this belongs to this group. Anyway, my question is as follows: I have a list (STL list) whose elements are pairs of integers (STL pairs, say objects of class T). When I create...
6
by: alan | last post by:
I'm creating a sort-of "wrapper" class which (partly) acts like a variable. Something like: template<class t> class cell{ t curval; public: /*public for debugging only - will be private in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.