Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Re: [lpc2000] Problems with constructor of static member class

2005-06-28 by thomas.kuschel@anton-paar.com

Hallo Søren,

When you declare a static data member within class, you are _not_ defining 
it. ( That is, your are not allocating strage for it)
Instead you must provide a global definition for it elsewhere, outside the 
class.
This is done by redeclaring the _static_ varialbe using the scope 
resolution operator to identify the class to which it belongs.
This causes storage for the varialbe to be allocated.
(Remember, a class definition is simply a logical construct that does not 
have physical reality - not so in your program)
Try not to use statics within classes - generally
also - if this program probably would work - it's not really good code.

Hope could help

Thomas Kuschel

================
Anton Paar GmbH
Research & Development
Anton-Paar-Str. 20
8054 Graz
Austria
Tel. +43 316 257-469

lpc2000@yahoogroups.com wrote on 28.06.2005 11:22:06:

> Hi all.
> 
> I suspect the GNUARM compiler to have a flaw, since the following code
> doesn't work as intended:
> 
> (Header)
> class A
> {
>     public:
>         A(int x);
>         int m_x;
>         static A* get_a_Instance();
> 
>     private:
>         static A m_a;
>         static A m_b;
> 
> };
> (Source)
> A A::m_a(1);
> A A::m_b(2);
> 
> A::A(int x)
> {
>     m_x = x;
> }
> 
> A* A::get_a_Instance()
> {
>    return &m_a;
> }
> 
> int main()
> {
>    A* temp_a = get_a_Instance();
>    int x = temp_a->m_x;
> }
> 
> The problem is that the constuctor doesn_t get called, causing m_x to
> have a garbage value (or 0). Even if I make an object of type A first,
> it doesn't work. 
> Is it something I have to setup in my startup.s file?
> 
> Best Regards
> Søren
> 
> 
> 
> 
> 
> 
> Yahoo! Groups Links
> 
> 
> 
> 
> 
> 
> 


[Non-text portions of this message have been removed]

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.