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(CMyCl assB* ptB);
};
/////////////////////////// fileA.cpp /////////////////////////
//
#include "fileA.h"
// constructor 1
CMyClassA::CMyC lassA(void){
// ... whatever ...
}
// constructor 2
CMyClassA::CMyC lassA(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::CMyC lassB(void){
CMyClassA B1; // no problem
CMyClassA B2(this); // no problem
}
int main(void){
// ... whatever ...
return 1;
} 5 1393
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::CMyC lassB(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; }
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
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
"kamil" <ka*******@xxxp oczta.onet.pl> wrote in message news:<#5******* ******@TK2MSFTN GP12.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::CMyC lassB(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.
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*******@xxxp oczta.onet.pl> wrote in message news:<#5******* ******@TK2MSFTN GP12.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(vo id); };
/////////////////////////// fileB.cpp //////////////////////////
#include "fileB.h"
CMyClassB::C MyClassB(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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 of
the text, the program is supposed to demonstrate the use of a copy
constructor function for the object created on the heap (because there
is a char* pointer variable in the instantiated class) and how you
would use the delete keyword with the...
|
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 size n. There are n!/(s!(n-s)!) ways to do this. Donald
E. Knuth gives several methods (algorithms) to generate all the
s-combinations in . In such procedure-oriented way, each
s-combination is processed while it's being generated. In some
|
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 the properties:
mode1, mode2, and mode3.
This seems simple but I can't quite figure it out...
Any ideas anyone?
|
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;
}
|
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 with my variables.
Would i get the object from the applicationstate on page_load? and then keep
the global variable in memory until a button is clicked or a certain event
takes place? But i dont know how to acess this variable if it is created in...
| |
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. consistently may make the code easier
to understand for someone else, etc. Using "this." makes it immediately
clear, for example, that the variable being referred to is a member of the
same class and is not declared in the method as a local variable.
...
|
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:
Dim objCurrentUser As WebsiteClass.CurrentUser =
Session("CurrentUser")
If objCurrentUser Is Nothing Then objCurrentUser = New
WebsiteClass.CurrentUser
Any page can be linked to directly and not force a login, so without
|
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 System.Object, ByVal e As
System.EventArgs) Handles OK.Click
Dim WebRequest As HttpWebRequest
Dim instance As HttpWebRequest =...
|
by: subramanian100in |
last post by:
Consider the following program:
#include <iostream>
using namespace std;
class Base
{
public:
Base(int x = 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |