J
joetekubi
hello all,
while working with STL containers and various class
composition, I can across a bug that I can't seem
to find.
When using a STL container in a derived class with
composition, everything is good. If I use a pointer
to a base class, the container fails with a SIGSEGV.
TIA for any assistance.
Example code is below
Platform: Redhat 9, compile with "g++ -g test.cpp"
//===================== cut here ================================
#include <iostream>
#include <set> // STL set container for nexthop IP addresses
class Carbo
{
private:
std::set < int > qtest;
std::set < int >::iterator qiter; // destination address iterator
public:
Carbo();
~Carbo();
void DoSomething();
};
class bread
{
private:
Carbo *pcarbo;
public:
bread();
~bread();
void DoBread();
};
class potato
{
private:
Carbo mycarbo;
public:
potato();
~potato();
void DoPotato();
};
using namespace std;
Carbo::Carbo(){ cout << "Carbo ctor" << endl; }
Carbo::~Carbo() { cout << "Carbo dtor" << endl; }
void Carbo:oSomething()
{
cout << "qtest before insert size = " << qtest.size() << endl;
qtest.insert(2);
qtest.insert(4);
qtest.insert(14);
qtest.insert(3);
cout << "qtest after insert size = " << qtest.size() << endl;
}
bread::bread()
{
cout << "bread ctor " << endl;
Carbo *pcarbo = new Carbo();
}
bread::~bread() { cout << " bread dtor" << endl; }
void bread:oBread() { pcarbo->DoSomething(); }
potato:otato() : mycarbo() { cout << "potato ctor " << endl; }
potato::~potato() { cout << " potato dtor" << endl; }
void potato:oPotato() { mycarbo.DoSomething(); }
int main()
{
potato sweetpotato;
sweetpotato.DoPotato(); // works ok, contained class
bread wholewheat;
wholewheat.DoBread(); // fails - pointer to class
return 0;
}
while working with STL containers and various class
composition, I can across a bug that I can't seem
to find.
When using a STL container in a derived class with
composition, everything is good. If I use a pointer
to a base class, the container fails with a SIGSEGV.
TIA for any assistance.
Example code is below
Platform: Redhat 9, compile with "g++ -g test.cpp"
//===================== cut here ================================
#include <iostream>
#include <set> // STL set container for nexthop IP addresses
class Carbo
{
private:
std::set < int > qtest;
std::set < int >::iterator qiter; // destination address iterator
public:
Carbo();
~Carbo();
void DoSomething();
};
class bread
{
private:
Carbo *pcarbo;
public:
bread();
~bread();
void DoBread();
};
class potato
{
private:
Carbo mycarbo;
public:
potato();
~potato();
void DoPotato();
};
using namespace std;
Carbo::Carbo(){ cout << "Carbo ctor" << endl; }
Carbo::~Carbo() { cout << "Carbo dtor" << endl; }
void Carbo:oSomething()
{
cout << "qtest before insert size = " << qtest.size() << endl;
qtest.insert(2);
qtest.insert(4);
qtest.insert(14);
qtest.insert(3);
cout << "qtest after insert size = " << qtest.size() << endl;
}
bread::bread()
{
cout << "bread ctor " << endl;
Carbo *pcarbo = new Carbo();
}
bread::~bread() { cout << " bread dtor" << endl; }
void bread:oBread() { pcarbo->DoSomething(); }
potato:otato() : mycarbo() { cout << "potato ctor " << endl; }
potato::~potato() { cout << " potato dtor" << endl; }
void potato:oPotato() { mycarbo.DoSomething(); }
int main()
{
potato sweetpotato;
sweetpotato.DoPotato(); // works ok, contained class
bread wholewheat;
wholewheat.DoBread(); // fails - pointer to class
return 0;
}