C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to create an initialised object declared as a class memb

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
CFF
Guest





PostPosted: Wed Aug 18, 2004 10:41 am    Post subject: How to create an initialised object declared as a class memb Reply with quote



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;
}
Back to top
kamil
Guest





PostPosted: Wed Aug 18, 2004 11:09 am    Post subject: Re: How to create an initialised object declared as a class Reply with quote




Try this


Quote:

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



Back to top
Karl Heinz Buchegger
Guest





PostPosted: Wed Aug 18, 2004 11:28 am    Post subject: Re: How to create an initialised object declared as a class Reply with quote



CFF wrote:
Quote:

/////////////////////////// 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
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Karl Heinz Buchegger
Guest





PostPosted: Wed Aug 18, 2004 11:30 am    Post subject: Re: How to create an initialised object declared as a class Reply with quote

Karl Heinz Buchegger wrote:
Quote:

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
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
CFF
Guest





PostPosted: Fri Aug 20, 2004 9:53 am    Post subject: Re: How to create an initialised object declared as a class Reply with quote

"kamil" <kamildobk (AT) xxxpoczta (DOT) onet.pl> wrote

Quote:
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.

Back to top
Michael Chisholm
Guest





PostPosted: Wed Sep 01, 2004 8:24 pm    Post subject: Re: How to create an initialised object declared as a class Reply with quote

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:
Quote:
"kamil" <kamildobk (AT) xxxpoczta (DOT) onet.pl> wrote


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.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.