Topic: About this and constructors
Author: "Bernard Tatin" <Bernard.Tatin@univ-orleans.fr>
Date: 1999/10/15 Raw View
You're right, I made mistakes in writing my sample code. The fact that using
'this' in a constructor is legal, I made research in another direction : my
classes are derived or use MFC classes. The problem come in a great part
from the Windows message loop, bad inheritance from MFC classes and bad use
of virtual inheritance.
I always have a warning from Insure++ even if I do
Mama::Mama()
: Fa2(0) // Fa2 no longer uninitialized.
{
Fa2 = new CA1(this);
// a lot of things
}
as AlanW suggested. Doing this and adding a virtual keyword in the right
place, I get only 1 occurence of this warning instead of 123.
Sometimes, it is very hard to make the difference between pure C++ misuse,
MFC misuse and Windows misuse, even with a lot of debugging tools. This code
is old and come from very different compilers : it does not help me.
Many Thanks to all of you.
Bernard Tatin
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: AllanW <allan_w@my-deja.com>
Date: 1999/10/14 Raw View
In article <7tk8s5$en9@centre.univ-orleans.fr>,
"Bernard Tatin" <Bernard.Tatin@univ-orleans.fr> wrote:
> I use some classes with something like that :
You made some errors when you simplified your code.
The result has undefined symbols in several places.
> class CMamaOfMama
> {
> public:
> CMamaOfMama() {...}
> virtual ~CMamaOfMama() {...}
> };
> class CA1;
You forward declared CA1, but you never do define it,
so ...
> class CMama : public CMamaOfMama
> {
> public:
> CMama();
> virtual ~CMama() {...}
> // a lot of things
> protected:
> CA2 *Fa2;
... so I'll assume you meant CA1 here and...
> // a lot of things
> };
> class CA1
> {
> public:
> CA1(CMama *m);
> virtual ~CA1() {...}
> // a lot of things
> protected:
> CMama *FMama;
> // a lot of things
> };
>
> CA1::CA1(CMama *m) : FMama(m)
> {
> }
>
> line 1 Mama::Mama()
> line 2 {
> line 3 // is this a good idea ?
> line 4 Fa2 = new CA2(this);
... and here.
> line 5 // a lot of things
> line 6 }
>
> What do you think about this code (line 4)?
This might generate a warning message, but it's legal.
Just make sure that CA2's constructor doesn't treat
the Mama* as pointing to a valid Mama object yet.
Saving the address for later is not a problem.
> What does the standard says about using 'this'
> in a constructor?
I don't have a copy with me.
> In this case what does the compiler :
> 1 :
> call CMamaOfMama::CMamaOfMama()
> call CMama::CMama()
> 2 :
> call CMama::CMama() witch calls
> CMamaOfMama::CMamaOfMama()
These are the same thing! That is, the compiler uses
some method to ensure that the CMamaOfMama sub-object
is completely constructed before CMama's constructor
begins to do it's magic. The details (stack return
addresses and so on) are unimportant and system-defined.
> I have Insure++ to debug memory problems. It tells me
> that I read some uninitialized memory in line 2. Have
> you an idea about this?
I've not used Insure++. Perhaps it's being misled by the
fact that you didn't use a memberwise initialization for
member Fa2. Instead, you initialized it in the body of
the constructor. This is perfectly alright, but to get
Insure++ to shut up try changing the code to this:
Mama::Mama()
: Fa2(0) // Fa2 no longer uninitialized.
{
Fa2 = new CA1(this);
// a lot of things
}
> Is there a link between the 3 problems?
It's hard to tell which 3 things you mentioned are problems,
especially with the inconsistent names. If my answer doesn't
make sense, try to fix your sample code and ask again.
--
Allan_W@my-deja.com is a "Spam Magnet," never read.
Please reply in newsgroups only, sorry.
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Bernard Tatin" <Bernard.Tatin@univ-orleans.fr>
Date: 1999/10/09 Raw View
I use some classes with something like that :
class CMamaOfMama
{
public:
CMamaOfMama() {...}
virtual ~CMamaOfMama() {...}
};
class CA1;
class CMama : public CMamaOfMama
{
public:
CMama();
virtual ~CMama() {...}
// a lot of things
protected:
CA2 *Fa2;
// a lot of things
};
class CA1
{
public:
CA1(CMama *m);
virtual ~CA1() {...}
// a lot of things
protected:
CMama *FMama;
// a lot of things
};
CA1::CA1(CMama *m) : FMama(m)
{
}
line 1 Mama::Mama()
line 2 {
line 3 // is this a good idea ?
line 4 Fa2 = new CA2(this);
line 5 // a lot of things
line 6 }
What do you think about this code (line 4)? What does the standard says
about using
'this' in a constructor?
In this case what does the compiler :
1 :
call CMamaOfMama::CMamaOfMama()
call CMama::CMama()
2 :
call CMama::CMama() witch calls CMamaOfMama::CMamaOfMama()
I have Insure++ to debug memory problems. It tells me that I read some
uninitialized memory in line 2. Have you an idea about this?
Is there a link between the 3 problems?
Thank you for your answers.
Bernard Tatin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/10/09 Raw View
Bernard Tatin wrote:
>
> I use some classes with something like that :
>
> class CMamaOfMama
> {
> public:
> CMamaOfMama() {...}
> virtual ~CMamaOfMama() {...}
> };
> class CA1;
> class CMama : public CMamaOfMama
> {
> public:
> CMama();
> virtual ~CMama() {...}
> // a lot of things
> protected:
You haven't defined CA2 anywhere.
> CA2 *Fa2;
> // a lot of things
> };
> class CA1
> {
> public:
> CA1(CMama *m);
> virtual ~CA1() {...}
> // a lot of things
> protected:
> CMama *FMama;
> // a lot of things
> };
>
> CA1::CA1(CMama *m) : FMama(m)
> {
> }
>
> line 1 Mama::Mama()
What is 'Mama'?
> line 2 {
> line 3 // is this a good idea ?
> line 4 Fa2 = new CA2(this);
I gather from line 4, that 'Mama' is a class, and that it's probably
derived from class 'CMama'.
> line 5 // a lot of things
> line 6 }
>
> What do you think about this code (line 4)? What does the standard says
I have no idea what the constructor of a CA2 looks like, what argument
types it takes, nor what it would do with 'this'. 'this' is a valid
pointer inside a constructor, but there are lots of invalid things you
can do with it. I can't tell whether CA2's constructor does any of those
things, because you haven't provided its definition.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/10/10 Raw View
In article <7tk8s5$en9@centre.univ-orleans.fr>, Bernard Tatin
<Bernard.Tatin@univ-orleans.fr> writes
>I use some classes with something like that :
>
>class CMamaOfMama
>{
> public:
> CMamaOfMama() {...}
> virtual ~CMamaOfMama() {...}
>};
>class CA1;
>class CMama : public CMamaOfMama
>{
> public:
> CMama();
> virtual ~CMama() {...}
> // a lot of things
> protected:
> CA2 *Fa2;
> // a lot of things
>};
>class CA1
>{
> public:
> CA1(CMama *m);
> virtual ~CA1() {...}
> // a lot of things
> protected:
> CMama *FMama;
> // a lot of things
>};
>
>
>CA1::CA1(CMama *m) : FMama(m)
>{
>}
>
>line 1 Mama::Mama()
>line 2 {
>line 3 // is this a good idea ?
>line 4 Fa2 = new CA2(this);
>line 5 // a lot of things
>line 6 }
>
>What do you think about this code (line 4)? What does the standard says
>about using
>'this' in a constructor?
Horrible and I think a disaster because I doubt that it does what you
intend, even when you remove the typos. Even if I am mistaken, such
code is so contorted that it would be a maintenance nightmare.
If your intent is to use placement new to avoid heap allocation you need
to provide a suitable buffer (char[ sizeof(CA2) + alignment extras]))
into which you can construct your CA2. And your dtor must now destroy,
not delete, the object.
>
>In this case what does the compiler :
> 1 :
> call CMamaOfMama::CMamaOfMama()
> call CMama::CMama()
> 2 :
> call CMama::CMama() witch calls CMamaOfMama::CMamaOfMama()
>
>I have Insure++ to debug memory problems. It tells me that I read some
>uninitialized memory in line 2. Have you an idea about this?
>
>Is there a link between the 3 problems?
Using complicated solutions to problems?
>
>Thank you for your answers.
>
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]