static object created via private ctor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Liu

    static object created via private ctor

    Hi,

    Can anyone explain why the following code works? The static object creation
    is via the private ctor. If I try to define a X object in the mainline, of
    course I will
    get complaint about calling the private ctor.

    I ran the code with CW8.3, it works fine. the output is:[color=blue]
    >Private ctor
    >dtor[/color]
    However, if I ran it with VC 6, the output is[color=blue]
    >Private ctor.[/color]

    Is it compiler problem?


    #include <iostream>

    class X{
    static X sx;
    X(){std::cout<< "Private ctor\n";}
    public:
    ~X(){std::cout< <"dtor\n";}
    };
    X X::sx;
    int main(){
    return 0;
    }

    Thanks

    Eric


  • John Harrison

    #2
    Re: static object created via private ctor


    "John Harrison" <john_andronicu s@hotmail.com> wrote in message
    news:bfnu4e$f2v 99$1@ID-196037.news.uni-berlin.de...[color=blue]
    >
    > "Eric Liu" <eric_nzeds@yah oo.com.au> wrote in message
    > news:3f1f7557@d news.tpgi.com.a u...[color=green]
    > > Hi,
    > >
    > > Can anyone explain why the following code works? The static object[/color]
    > creation[color=green]
    > > is via the private ctor. If I try to define a X object in the mainline,[/color][/color]
    of[color=blue][color=green]
    > > course I will
    > > get complaint about calling the private ctor.
    > >[/color]
    >
    > Well X::sx is inside the class so it can call the private dtor.
    >[/color]

    I meant ctor, not dtor.

    john


    Comment

    • Eric Liu

      #3
      Re: static object created via private ctor

      > I meant ctor, not dtor.[color=blue]
      >
      > john[/color]

      Thanks John

      Eric


      Comment

      Working...