using initializer list

Discussion in 'C++' started by Tony Johansson, Nov 6, 2005.

  1. Hello!

    Is it possible to put the body below which is the instansiating of Test in
    the initialize list in some way.

    CEx07aView::CEx07aView()
    {
    m_pDlg = new Test(this);
    }

    //Tony
     
    Tony Johansson, Nov 6, 2005
    #1
    1. Advertising

  2. * Tony Johansson:
    >
    > Is it possible to put the body below which is the instansiating of Test in
    > the initialize list in some way.
    >
    > CEx07aView::CEx07aView()
    > {
    > m_pDlg = new Test(this);
    > }


    You have asked a lot of such homework questions earlier.

    Please consult your textbook.

    Cheers.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 6, 2005
    #2
    1. Advertising

  3. Tony Johansson wrote:
    > Hello!
    >
    > Is it possible to put the body below which is the instansiating of Test in
    > the initialize list in some way.
    >
    > CEx07aView::CEx07aView()
    > {
    > m_pDlg = new Test(this);
    > }
    >


    Simple

    CEx07aView::CEx07aView() : m_pDlg(new Test(this))
    {
    }

    But some compilers might give you warnings with this code. This is
    because you are using 'this' before the object it is refering to has
    been constructed, which is potentially a dangerous situation. I think I
    would usually prefer this code

    CEx07aView::CEx07aView() : m_pDlg(0)
    {
    m_pDlg = new Test(this);
    }

    The main difference between all three sets of code is what would happen
    if new Test(this) threw an exception.

    john
     
    John Harrison, Nov 7, 2005
    #3
  4. Tony Johansson

    Rolf Magnus Guest

    John Harrison wrote:

    >> CEx07aView::CEx07aView()
    >> {
    >> m_pDlg = new Test(this);
    >> }


    ....

    > CEx07aView::CEx07aView() : m_pDlg(new Test(this))
    > {
    > }


    ....

    > CEx07aView::CEx07aView() : m_pDlg(0)
    > {
    > m_pDlg = new Test(this);
    > }
    >
    > The main difference between all three sets of code is what would happen
    > if new Test(this) threw an exception.


    How are they different in that regard?
     
    Rolf Magnus, Nov 7, 2005
    #4
  5. * Rolf Magnus:
    > John Harrison wrote:
    >
    > >> CEx07aView::CEx07aView()
    > >> {
    > >> m_pDlg = new Test(this);
    > >> }

    >
    > ...
    >
    > > CEx07aView::CEx07aView() : m_pDlg(new Test(this))
    > > {
    > > }

    >
    > ...
    >
    > > CEx07aView::CEx07aView() : m_pDlg(0)
    > > {
    > > m_pDlg = new Test(this);
    > > }
    > >
    > > The main difference between all three sets of code is what would happen
    > > if new Test(this) threw an exception.

    >
    > How are they different in that regard?


    Consider:

    struct CEx07aView;

    struct Test
    {
    Test( CEx07aView* ){ throw 666; }
    };

    struct FooBar
    {
    FooBar() { std::cout << "1\n"; }
    ~FooBar() { std::cout << "2\n"; }
    };

    struct CEx07aView
    {
    Test* m_pDlg;
    FooBar foo;

    ...
    };

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 7, 2005
    #5
    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. Razvan
    Replies:
    7
    Views:
    17,664
    Lee Fesperman
    Jul 4, 2004
  2. Aman
    Replies:
    4
    Views:
    439
    Agent Mulder
    Sep 15, 2003
  3. Alexander Stippler
    Replies:
    3
    Views:
    450
    Leor Zolman
    Apr 9, 2004
  4. Replies:
    2
    Views:
    234
    Keith Thompson
    Aug 6, 2006
  5. Angel Tsankov
    Replies:
    10
    Views:
    521
    James Kanze
    May 8, 2007
Loading...

Share This Page