473,408 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

CFF
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;
}
Jul 22 '05 #1
5 1378

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

Jul 22 '05 #2
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
kb******@gascad.at
Jul 22 '05 #3
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
kb******@gascad.at
Jul 22 '05 #4
CFF
"kamil" <ka*******@xxxpoczta.onet.pl> wrote in message news:<#5*************@TK2MSFTNGP12.phx.gbl>...
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.
Jul 22 '05 #5
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" <ka*******@xxxpoczta.onet.pl> wrote in message news:<#5*************@TK2MSFTNGP12.phx.gbl>...
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.

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: August1 | last post by:
This is a short program that I have written from a text that demonstrates a class object variable created on the stack memory and another class object variable created on the heap memory. By way...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
9
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
2
by: Joshua Weir | last post by:
Hi, I have setup my asp.net app so that i have one globalobject that contains all information for my application. I am trying to work out the best way to implement the applicationstate object...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
11
by: syssyx | last post by:
I have a "CurrentUser" object in session that I want to access from each page of a vb.net website. I can successfully access everything if I include the following at the start of each pageload: ...
3
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I'm trying to create a call to a web page to validate and register software. The code I'm using is: Private Sub OK_Click(ByVal sender As...
3
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.