K
keith
Hi, I'm having weird run-time errors and it's clearly something I'm
doing wrong, but I don't see what's going on.
Class B has a pointer to a structure containing a bunch of vars, and
an object of class A as shown in the code in the first example below.
Everything works fine when I take the A object out of the private
structure and put the object itself inside B as in the second example,
but when I do it as shown in the first example, 'bad stuff happens' -
looks a bit like heap corruption, but I haven't been able to pin it
down yet.
Any ideas or whacks with the clue-bat appreciated.
//--------------THIS VERSION FAILS!
// b.h----------------------------
struct BPrivate;
class B
{
private:
struct BPrivate* p_;
public:
B();
~B();
}
// b.cc----------------------------
#include "a.h"
#include "b.h"
struct BPrivate
{
int var1;
int var2;
A Aobject;
};
B::B() : p_(new BPrivate()) {} //<<should invoke A's default c-tor,
no?
B::~B() { delete p_; } // likewise, should call A's d-tor?
//--------------THIS VERSION WORKS!
// b.h----------------------------
struct BPrivate;
class B
{
private:
struct BPrivate* p_;
A Aobject;
public:
B();
~B();
}
// b.cc----------------------------
#include "a.h"
#include "b.h"
struct BPrivate
{
int var1;
int var2;
};
B::B() : p_(new BPrivate()), Aobject() {}
B::~B() { delete p_; }
doing wrong, but I don't see what's going on.
Class B has a pointer to a structure containing a bunch of vars, and
an object of class A as shown in the code in the first example below.
Everything works fine when I take the A object out of the private
structure and put the object itself inside B as in the second example,
but when I do it as shown in the first example, 'bad stuff happens' -
looks a bit like heap corruption, but I haven't been able to pin it
down yet.
Any ideas or whacks with the clue-bat appreciated.
//--------------THIS VERSION FAILS!
// b.h----------------------------
struct BPrivate;
class B
{
private:
struct BPrivate* p_;
public:
B();
~B();
}
// b.cc----------------------------
#include "a.h"
#include "b.h"
struct BPrivate
{
int var1;
int var2;
A Aobject;
};
B::B() : p_(new BPrivate()) {} //<<should invoke A's default c-tor,
no?
B::~B() { delete p_; } // likewise, should call A's d-tor?
//--------------THIS VERSION WORKS!
// b.h----------------------------
struct BPrivate;
class B
{
private:
struct BPrivate* p_;
A Aobject;
public:
B();
~B();
}
// b.cc----------------------------
#include "a.h"
#include "b.h"
struct BPrivate
{
int var1;
int var2;
};
B::B() : p_(new BPrivate()), Aobject() {}
B::~B() { delete p_; }