Not able to understand why segmentation fault is coming while accessing const function from inner class.
Program:
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
typedef struct
{
double a;
double b;
} conf;
class configuration
{
public:
configuration(const conf *conf) : conf_(conf) {}
double
getA() const
{
return conf_->a;
}
double
getB() const
{
return conf_->b;
}
private:
const conf *conf_;
};
class Test
{
private:
class TestInner
{
public:
TestInner(const configuration *conf) : conf_(conf) {}
void
print()
{
cout << "INNER START\n";
cout << "Innerclass data: a : " << conf_->getA()
<< "b : " << conf_->getB() << "\n";
cout << "INNER END\n";
}
private:
const configuration *conf_;
};
private:
const configuration *conf_;
typedef std::unordered_map<std::string, TestInner &> TestInners;
TestInners testInners_;
const std::string key_;
public:
Test(const configuration *conf);
void
print();
};
Test::Test(const configuration *conf) : conf_(conf), key_("ABC")
{
TestInner testInner_(conf);
testInners_.insert(std::pair<std::string, TestInner &>(key_, testInner_));
}
void
Test::print()
{
cout << "Test data: a : " << conf_->getA() << "b : " << conf_->getB()
<< "\n";
TestInners::const_iterator testInner = testInners_.find(key_);
testInner->second.print();
}
int
main()
{
conf conf_;
conf_.a = 10;
conf_.b = 10;
configuration configuration_(&conf_);
Test test(&configuration_);
test.print();
cout << "Hello World";
return 0;
}
Output:
Test data: a : 10b : 10
INNER START
Segmentation fault (core dumped)
Aucun commentaire:
Enregistrer un commentaire