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

Static data member initialization

P: n/a
Sam
Hi,
I need to initialize a static data member by calling a method of another
static member. Like:

class A
{
public:
static int TYPE;
private
static Type _s_type;
};

int A::TYPE = _s_type.ID();
Type A::_s_type;

I compiled the code with my compiler. But I'm not sure whether it complies
with the standard.

Thanks.
Sam
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Sam wrote:
Hi,
I need to initialize a static data member by calling a method of
another static member. Like:

class A
{
public:
static int TYPE;
private
static Type _s_type;
};

int A::TYPE = _s_type.ID();
Type A::_s_type;

I compiled the code with my compiler. But I'm not sure whether it
complies with the standard.


I complies in a sense that it's well-formed. However, it has undefined
behaviour because when you try to initialise A::TYPE, A::_s_type, which
you need, hasn't been initialised properly yet. Reorder their definitions.

V
Jul 23 '05 #2

P: n/a
Sam
> I complies in a sense that it's well-formed. However, it has undefined
behaviour because when you try to initialise A::TYPE, A::_s_type, which
you need, hasn't been initialised properly yet. Reorder their
definitions.

V

Thank you very much for your reply. Do you mean that it works well as long
as _s_type is defined before TYPE?
Type A::_s_type;
int A::TYPE = _s_type.ID();
I was wondering what the standard says about this.

Sam
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.