Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Static data member initialization

Reply
Thread Tools

Static data member initialization

 
 
Sam
Guest
Posts: n/a
 
      04-16-2005
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-16-2005
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


 
Reply With Quote
 
 
 
 
Sam
Guest
Posts: n/a
 
      04-17-2005
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
initialization of a const static float data member in a class akomiakov@gmail.com C++ 23 06-15-2008 07:58 PM
initialization of static data member in header file mathieu C++ 7 02-12-2008 09:01 AM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
static data member initialization Mike - EMAIL IGNORED C++ 3 02-11-2006 09:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57