How to create an initialised object declared as a class member variable?

Discussion in 'C++' started by CFF, Aug 18, 2004.

  1. CFF

    CFF Guest

    I am working on a VC6++ project that involve an object to be initialised
    by a 'this' pointer pointing to another object. I encountered, however,
    a syntax error. I wonder if someone can help. See the code below. What I
    couldn't figure out is it is OK with

    CMyClassA B2(this);

    in fileB.cpp but a SYNTAX ERROR with

    CMyClassA m_B2(this);

    in fileB.h. What should I do in order to create a initialised object of
    CMyClassA if I really need a class member variable (m_B2) rather than a local (B2)?

    Thanks for any help.


    /////////////////////////// fileA.h ///////////////////////////
    //
    // forward declaration
    class CMyClassB;

    // CMyClassA declaration
    class CMyClassA{
    public:
    CMyClassB* m_ptA;

    public:
    CMyClassA();
    CMyClassA(CMyClassB* ptB);
    };


    /////////////////////////// fileA.cpp /////////////////////////
    //
    #include "fileA.h"

    // constructor 1
    CMyClassA::CMyClassA(void){
    // ... whatever ...
    }

    // constructor 2
    CMyClassA::CMyClassA(CMyClassB* ptB){
    m_ptA = ptB;
    }


    /////////////////////////// fileB.h ///////////////////////////
    //
    #include "fileA.h"

    class CMyClassB{
    public:
    CMyClassA m_B1; // no problem
    CMyClassA m_B2(this); // syntax error : 'this', WHY???

    public:
    CMyClassB(void);
    };


    /////////////////////////// fileB.cpp //////////////////////////

    #include "fileB.h"

    CMyClassB::CMyClassB(void){
    CMyClassA B1; // no problem
    CMyClassA B2(this); // no problem
    }


    int main(void){
    // ... whatever ...
    return 1;
    }
     
    CFF, Aug 18, 2004
    #1
    1. Advertising

  2. CFF

    kamil Guest

    Try this


    >
    > /////////////////////////// fileB.h ///////////////////////////
    > //
    > #include "fileA.h"
    >
    > class CMyClassB{
    > public:
    > CMyClassA m_B1; // no problem
    > CMyClassA m_B2; // we won't call constructor here
    >
    > public:
    > CMyClassB(void);
    > };
    >
    >
    > /////////////////////////// fileB.cpp //////////////////////////
    >
    > #include "fileB.h"
    >
    > CMyClassB::CMyClassB(void)
    > : m_B2(this) // we will call it here instead ( hope there is no problem

    with passing 'this' pointing to not fully constructed object )
    >{
    > CMyClassA B1; // no problem
    > CMyClassA B2(this); // no problem
    > }
    >
    >
    > int main(void){
    > // ... whatever ...
    > return 1;
    > }
     
    kamil, Aug 18, 2004
    #2
    1. Advertising

  3. Re: How to create an initialised object declared as a class membervariable?

    CFF wrote:
    >
    > /////////////////////////// fileB.h ///////////////////////////
    > //
    > #include "fileA.h"
    >
    > class CMyClassB{
    > public:
    > CMyClassA m_B1; // no problem
    > CMyClassA m_B2(this); // syntax error : 'this', WHY???


    Because this is not the way initializations are specified in C++
    What you need is a constructor for CMyClassB, which does the initialization


    CMyClassB() : m_B2( this ) {}


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 18, 2004
    #3
  4. Re: How to create an initialised object declared as a class membervariable?

    Karl Heinz Buchegger wrote:
    >
    > CFF wrote:
    > >
    > > /////////////////////////// fileB.h ///////////////////////////
    > > //
    > > #include "fileA.h"
    > >
    > > class CMyClassB{
    > > public:
    > > CMyClassA m_B1; // no problem
    > > CMyClassA m_B2(this); // syntax error : 'this', WHY???

    >
    > Because this is not the way initializations are specified in C++
    > What you need is a constructor for CMyClassB, which does the initialization
    >
    > CMyClassB() : m_B2( this ) {}


    Oh. I see you already have a constructor. So add the initialization
    to that constructor


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 18, 2004
    #4
  5. CFF

    CFF Guest

    "kamil" <> wrote in message news:<#5#>...
    > Try this
    >
    >
    > >
    > > /////////////////////////// fileB.h ///////////////////////////
    > > //
    > > #include "fileA.h"
    > >
    > > class CMyClassB{
    > > public:
    > > CMyClassA m_B1; // no problem
    > > CMyClassA m_B2; // we won't call constructor here
    > >
    > > public:
    > > CMyClassB(void);
    > > };
    > >
    > >
    > > /////////////////////////// fileB.cpp //////////////////////////
    > >
    > > #include "fileB.h"
    > >
    > > CMyClassB::CMyClassB(void)
    > > : m_B2(this) // we will call it here instead ( hope there is no problem

    > with passing 'this' pointing to not fully constructed object )
    > >{
    > > CMyClassA B1; // no problem
    > > CMyClassA B2(this); // no problem
    > > }
    > >
    > >
    > > int main(void){
    > > // ... whatever ...
    > > return 1;
    > > }


    Thank you for your help. I've tried it out and it works. But ... I am
    getting a warning message saying

    "warning C4355: 'this' : used in base member initializer list"

    when complied. I am a bit worry about this. Any idea about what's the
    problem with it? Does it cause any undersirable effect? Thanks again.
     
    CFF, Aug 20, 2004
    #5
  6. Re: How to create an initialised object declared as a class membervariable?

    I think as long as you don't try to manipulate the half-constructed
    object, you're fine. I believe I do this very thing in some piece of
    software I wrote, and I have seen no ill effects. The compiler is just
    reminding you... you can always disable the warning with a #pragma.

    CFF wrote:
    > "kamil" <> wrote in message news:<#5#>...
    >
    >>Try this
    >>
    >>
    >>
    >>>/////////////////////////// fileB.h ///////////////////////////
    >>>//
    >>>#include "fileA.h"
    >>>
    >>>class CMyClassB{
    >>>public:
    >>>CMyClassA m_B1; // no problem
    >>>CMyClassA m_B2; // we won't call constructor here
    >>>
    >>>public:
    >>>CMyClassB(void);
    >>>};
    >>>
    >>>
    >>>/////////////////////////// fileB.cpp //////////////////////////
    >>>
    >>>#include "fileB.h"
    >>>
    >>>CMyClassB::CMyClassB(void)
    >>>: m_B2(this) // we will call it here instead ( hope there is no problem

    >>
    >> with passing 'this' pointing to not fully constructed object )
    >>
    >>>{
    >>>CMyClassA B1; // no problem
    >>>CMyClassA B2(this); // no problem
    >>>}
    >>>
    >>>
    >>>int main(void){
    >>>// ... whatever ...
    >>>return 1;
    >>>}

    >
    >
    > Thank you for your help. I've tried it out and it works. But ... I am
    > getting a warning message saying
    >
    > "warning C4355: 'this' : used in base member initializer list"
    >
    > when complied. I am a bit worry about this. Any idea about what's the
    > problem with it? Does it cause any undersirable effect? Thanks again.
     
    Michael Chisholm, Sep 1, 2004
    #6
    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. JohnZing

    declared or not declared ?

    JohnZing, Feb 5, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,596
    Jon Paal
    Feb 5, 2006
  2. Andy Fish
    Replies:
    4
    Views:
    2,423
    Andy Fish
    Dec 17, 2003
  3. Anon
    Replies:
    39
    Views:
    2,123
    Joona I Palaste
    May 2, 2004
  4. Thomas Hawtin

    static field initialised twice

    Thomas Hawtin, Apr 27, 2006, in forum: Java
    Replies:
    6
    Views:
    630
    Chris Smith
    Apr 28, 2006
  5. claire.bell1

    Initialised class member objects

    claire.bell1, Jul 21, 2003, in forum: C++
    Replies:
    2
    Views:
    430
    John Harrison
    Jul 22, 2003
Loading...

Share This Page