Static data member initialization

S

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
 
V

Victor Bazarov

Sam said:
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
 
S

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top