static object created via private ctor

Discussion in 'C++' started by John Harrison, Jul 24, 2003.

  1. "John Harrison" <> wrote in message
    news:bfnu4e$f2v99$-berlin.de...
    >
    > "Eric Liu" <> wrote in message
    > news:...
    > > 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.
    > >

    >
    > Well X::sx is inside the class so it can call the private dtor.
    >


    I meant ctor, not dtor.

    john
    John Harrison, Jul 24, 2003
    #1
    1. Advertising

  2. John Harrison

    Eric Liu Guest

    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:
    >Private ctor
    >dtor

    However, if I ran it with VC 6, the output is
    >Private ctor.


    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
    Eric Liu, Jul 25, 2003
    #2
    1. Advertising

  3. John Harrison

    Eric Liu Guest

    > I meant ctor, not dtor.
    >
    > john


    Thanks John

    Eric
    Eric Liu, Jul 25, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apricot
    Replies:
    4
    Views:
    505
    velthuijsen
    Apr 16, 2004
  2. NVH
    Replies:
    8
    Views:
    473
    mlimber
    Jul 6, 2006
  3. Grizlyk
    Replies:
    8
    Views:
    457
    Grizlyk
    Nov 29, 2006
  4. John Salmon
    Replies:
    3
    Views:
    437
    Victor Bazarov
    Dec 11, 2006
  5. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    394
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
Loading...

Share This Page