Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:00 PM
CFF
Guest
 
Posts: n/a
Default How to create an initialised object declared as a class member variable?

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;
}

  #2  
Old July 22nd, 2005, 06:00 PM
kamil
Guest
 
Posts: n/a
Default Re: How to create an initialised object declared as a class member variable?


Try this

[color=blue]
>
> /////////////////////////// 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[/color]
with passing 'this' pointing to not fully constructed object )[color=blue]
>{
> CMyClassA B1; // no problem
> CMyClassA B2(this); // no problem
> }
>
>
> int main(void){
> // ... whatever ...
> return 1;
> }[/color]


  #3  
Old July 22nd, 2005, 06:00 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default Re: How to create an initialised object declared as a class membervariable?

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

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
kbuchegg@gascad.at
  #4  
Old July 22nd, 2005, 06:00 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default Re: How to create an initialised object declared as a class membervariable?

Karl Heinz Buchegger wrote:[color=blue]
>
> CFF wrote:[color=green]
> >
> > /////////////////////////// fileB.h ///////////////////////////
> > //
> > #include "fileA.h"
> >
> > class CMyClassB{
> > public:
> > CMyClassA m_B1; // no problem
> > CMyClassA m_B2(this); // syntax error : 'this', WHY???[/color]
>
> 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 ) {}[/color]

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


--
Karl Heinz Buchegger
kbuchegg@gascad.at
  #5  
Old July 22nd, 2005, 06:04 PM
CFF
Guest
 
Posts: n/a
Default Re: How to create an initialised object declared as a class member variable?

"kamil" <kamildobk@xxxpoczta.onet.pl> wrote in message news:<#5#djPRhEHA.704@TK2MSFTNGP12.phx.gbl>...[color=blue]
> Try this
>
>[color=green]
> >
> > /////////////////////////// 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[/color]
> with passing 'this' pointing to not fully constructed object )[color=green]
> >{
> > CMyClassA B1; // no problem
> > CMyClassA B2(this); // no problem
> > }
> >
> >
> > int main(void){
> > // ... whatever ...
> > return 1;
> > }[/color][/color]

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.
  #6  
Old July 22nd, 2005, 06:29 PM
Michael Chisholm
Guest
 
Posts: n/a
Default 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:[color=blue]
> "kamil" <kamildobk@xxxpoczta.onet.pl> wrote in message news:<#5#djPRhEHA.704@TK2MSFTNGP12.phx.gbl>...
>[color=green]
>>Try this
>>
>>
>>[color=darkred]
>>>/////////////////////////// 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[/color]
>>
>> with passing 'this' pointing to not fully constructed object )
>>[color=darkred]
>>>{
>>>CMyClassA B1; // no problem
>>>CMyClassA B2(this); // no problem
>>>}
>>>
>>>
>>>int main(void){
>>>// ... whatever ...
>>>return 1;
>>>}[/color][/color]
>
>
> 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.[/color]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.