Virtual function not overloading in derived class [duplicate] - c++

This question already has answers here:
What is object slicing?
(18 answers)
Closed 2 years ago.
class CommandRoot {
protected:
string cmdString = "";
string title = "";
string tags = "";
string description = "";
public:
string getCmdString() {
return cmdString;
}
string getTitle() {
return title;
}
string getTags() {
return tags;
}
string getDescription() {
return description;
}
virtual bool onTrigger() {
return 1;
}
};
class CmdFirst : public CommandRoot{
public:
CmdFirst() {
cmdString = "testing1";
title = "";
tags = "";
description = "";
}
bool onTrigger() {
cout << "C";
return 0;
}
};
class Player {
NPC *target = NULL;
CommandRoot *cmdList[1];
public:
Player() {
cmdList[0] = new CmdFirst();
}
CommandRoot getCmdList(int n) {
return *cmdList[n];
}
NPC getTarget() {
return *target;
}
bool setTarget(NPC* t) {
target = t;
return 0;
}
string listen() {
string cmd = "";
cin >> cmd;
return cmd;
}
};
int main()
{
std::cout << "Hello World!\n";
Player* player = new Player();
NPC* firstNPC = new NPC();
player->setTarget(firstNPC);
bool exit = false;
do {
if (player->listen().compare(player->getCmdList(0).getCmdString()) == 0) {
cout << "A";
cout << player->getCmdList(0).onTrigger();
}
else
{
cout << "B";
}
} while (exit == false);
}
The following line is calling the parent's virtual function instead of the derived class.
cout << player->getCmdList(0).onTrigger();
I have a feeling it's because the data type of the array is the parent class, but that shouldn't prevent the array element from being assigned the data type of the derived class, then calling that class's function.

It's because CommandRoot getCmdList(int n) returns a CommandRoot object. For virtual functions to work you need a reference or a pointer.
Try changing to
CommandRoot& getCmdList(int n) {
return *cmdList[n];
}

Related

Returning an Array of pointers

I have to finish a prompt tonight for my coding class, and I believe I am writing this incorrectly. I have reviewed other stackoverflow answers, but I still lack the understanding necessary to correctly solve this. This is part of a bigger project, so I have included all code in case another function is causing this error, but in my testing only the Ballot class is giving me errors.
In getVote, if the parameter is within the used portion of the array,
return the Voter pointer that corresponds with that position.
This is supposed to be tested with:
Ballot ballot1("WI-643UWO");
const Vote *vote3 = ballot1.getVote(2);
cout << vote3 << "\n";
//header file
pragma once
#include <string>
using namespace std;
class Election {
private:
string office;
string firstCanidiateName;
string secondCanidiateName;
public:
Election(string office2, string firstCanidiateName2, string secondCanidiateName2);
string getOffice() const;
string getCandidate1() const;
string getCandidate2() const;
};
class Vote {
private:
string vOffice;
string canidiateName;
bool voteMadeInPerson;
public:
Vote(string vOffice2, string canidiateName2, bool voteMadeInPerson2);
string getOffice() const;
string getCandidate() const;
bool wasInPerson() const;
};
class Ballot {
private:
string voterID;
int votesStored;
Vote* votePointer[6];
public:
Ballot();
Ballot(string voterID2);
~Ballot();
string getVoterId() const;
int getVoteCount() const;
const Vote* getVote(int votePosition) const;
void recordVote(string office, string candidateName, bool voteInPerson);
int countInPersonVotes();
int findVote(string office) const;
};
//functions file
#include "p3.h"
#include <string>
using namespace std;
Election::Election(string office2, string firstCanidiateName2, string secondCanidiateName2) {
office = office2;
firstCanidiateName = firstCanidiateName2;
secondCanidiateName = secondCanidiateName2;
}
string Election::getOffice() const {
return office;
}
string Election::getCandidate1() const {
return firstCanidiateName;
}
string Election::getCandidate2() const {
return secondCanidiateName;
}
string Vote::getOffice() const {
return vOffice;
}
string Vote::getCandidate() const {
return canidiateName;
}
bool Vote::wasInPerson() const {
return voteMadeInPerson;
}
Vote::Vote(string vOffice2, string canidiateName2, bool voteMadeInPerson2) {
if (vOffice2.empty() == true) {
vOffice2 = "Unknown";
}
vOffice = vOffice2;
if (canidiateName2.empty() == true) {
canidiateName2 = "Write In";
}
canidiateName = canidiateName2;
voteMadeInPerson = voteMadeInPerson2;
}
string Ballot::getVoterId() const {
return voterID;
}
int Ballot::getVoteCount() const {
return votesStored;
}
const Vote* Ballot::getVote(int votePosition) const {
Vote* out;
if (votePosition > -1 && votePosition < votesStored) {
//unsure if this is proper syntax -- will need testing
out = votePointer[votePosition];
return out;
}
else {
out = nullptr;
return out;
}
}
void Ballot::recordVote(string office, string candidateName, bool voteInPerson) {
if ((votesStored + 1) < 6) {
int x = findVote(office);
if (x != -1) {
Vote* z = new Vote(office, candidateName, voteInPerson);
votePointer[votesStored] = z;
++votesStored;
}
}
}
int Ballot::countInPersonVotes() {
int count = 0;
for (int i = 0; i < votesStored; ++i) {
bool x = false;
x = votePointer[i]->wasInPerson();
if (x == true) {
count++;
}
}
return count;
}
int Ballot::findVote(string office) const {
//test to make sure this logically works
int i = 0;
bool matchCheck = false;
for (i = 0; i < votesStored; ++i) {
if (votePointer[i]->getOffice() == office) {
matchCheck = true;
return i + 1;
}
}
if (matchCheck == false) {
return -1;
}
}
Ballot::Ballot(string voterID2) {
voterID = voterID2;
votesStored = 0;
}
Ballot::Ballot() {
voterID = "Invalid ID";
votesStored = 0;
}
Ballot::~Ballot() {
/**this is what the prompt asks for, technically.
the book details that this can be achieved in a more simple matter --
using the delete[] operator.
*/
for (int i = 0; i < 6; ++i) {
delete votePointer[i];
}
}
Any help would be appreciated - I am unsure where I am messing up here. I seem to be getting a lot of read access violation errors, but we haven't touched on debugging, so I'm not necessarily sure how to identify what I did wrong. Furthermore, my functions did seem to work initially until I tried fixing it, and now it gives me a read-error almost instantly, which is concerning. And lastly - I am new to pointers, so there is a very high chance I messed up there. Thanks for any advice.

munmap_chunk(): invalid pointer (core dumped)

Code actually runs fine for me, but I have to submit it to someone, and in return I get an error which I cannot perceive.
The following error is:
munmap_chunk(): invalid pointer (core dumped)
The error appears to be occurring in method named: loadLuggage. I have also checked the code with Valgrind, but no possible leaks were detected. Perhaps I was not compiling it correctly.
#include <iostream>
#include <string>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <cstdlib>
#include <ctime>
using namespace std;
template<typename T>
int izbrisiVrsto(queue<T> &queue) {
int numberOfRemoved = 0;
while (!queue.empty()) {
queue.pop();
numberOfRemoved++;
}
return numberOfRemoved;
}
template<typename T>
int deleteStack(stack<T> &stack) {
int numberOfDeleted = 0;
while (!stack.empty()) {
stack.pop();
numberOfDeleted++;
}
return numberOfDeleted;
}
class Traveler {
private:
string name;
string surname;
int age;
string emso; //used as ID
public:
Traveler(const string &name, const string &surname, int age, const string &emso)
: name(name), surname(surname), age(age), emso(emso) {
}
~Traveler() {}
string getName() const {
return name;
}
void setName(const string &name) {
this->name = name;
}
string getSurname() const {
return surname;
}
void setSurname(const string &surname) {
this->surname = surname;
}
int getAge() const {
return age;
}
void setAge(int age) {
this->age = age;
}
string getEmso() const {
return emso;
}
void setEmso(const string &emso) {
this->emso = emso;
}
};
enum TravelClass {
ECONOMY,
BUSINESS
};
class Luggage {
private:
int luggageID;
public:
Luggage(int id) : luggageID(id) {}
~Luggage() {}
int getLuggageID() const {
return luggageID;
}
void setLuggageID(int id) {
this->luggageID = id;
}
};
class BoardingTicket {
private:
Traveler traveler;
TravelClass travelClass;
Luggage luggage;
public:
BoardingTicket(const Traveler &traveler, const TravelClass &pr, const Luggage &luggage)
: traveler(traveler), travelClass(pr), luggage(luggage) {
}
~BoardingTicket() {}
Traveler getTraveler() const {
return traveler;
}
void setTraveler(const Traveler &traveler) {
this->traveler = traveler;
}
TravelClass getTravelClass() const {
return travelClass;
}
void setTravelClass(TravelClass travelClass) {
this->travelClass = travelClass;
}
Luggage getLuggage() const {
return luggage;
}
void setLuggage(const Luggage &luggage) {
this->luggage = luggage;
}
};
class Plane {
private:
int id; //plane ID
public:
stack<stack<Luggage> > trunk; //stack of luggage on plane = all travelers luggage
private:
unsigned int maxNumberOfLuggage; //number of maxium luggage on the heap
public:
Plane(int id, unsigned int maxNumberOfLuggage) :
id(id),
maxNumberOfLuggage(maxNumberOfLuggage) {
}
~Plane() {}
//get/set metode
int getID() const {
return id;
}
void setID(int id) {
this->id = id;
}
unsigned int getMaxNumberOfLuggage() const {
return maxNumberOfLuggage;
}
void setMaxNumberOfLuggage(unsigned int maxNumberOfLuggage) {
this->maxNumberOfLuggage = maxNumberOfLuggage;
}
void loadLuggage(queue<Luggage> &luggages) {
//TODO: implementation
while (luggages.empty() == false) {
stack<Luggage> pallet; //certain number of luggage on pallet
for (int i = 0; i < this->maxNumberOfLuggage; i++) {
Luggage luggage = luggages.front();
pallet.push(luggage);
luggages.pop();
}
this->trunk.push(pallet); //loading in trunk
}
}
queue<Luggage> unloadLuggage() {
//TODO: implementation
queue<Luggage> unloadingLuggage;
while (trunk.empty() == false) {
stack<Luggage> pallet = trunk.top();
trunk.pop();
for (int i = 0; i < pallet.size(); i++) {
unloadingLuggage.push(pallet.top());
pallet.pop();
}
}
return unloadingLuggage; //HACK: dummy return
}
};
class Airline {
public:
queue<BoardingTicket> boardingPeople; //people waiting to board on a plane
queue<Luggage> unloadedLuggage;
Airline() {}
~Airline() {}
void split(queue<BoardingTicket> &travelers, queue<BoardingTicket> &businessTravelers,
queue<BoardingTicket> &economyTravelers) {
//TODO: implementation
while (travelers.empty() == false) {
BoardingTicket traveler = travelers.front(); //izbira prvega potnika
if (traveler.getTravelClass() == ECONOMY) {
economyTravelers.push(traveler);
} else if (traveler.getTravelClass() == BUSINESS) {
businessTravelers.push(traveler);
}
travelers.pop(); //traveler vkrcan
}
}
queue<BoardingTicket> merge(queue<BoardingTicket> &businessTravelers, queue<BoardingTicket> &economyTravelers) {
//TODO: implementation
queue<BoardingTicket> merged;
while (economyTravelers.empty() == false) {
BoardingTicket first = economyTravelers.front();
merged.push(first);
economyTravelers.pop();
}
while (businessTravelers.empty() == false) {
BoardingTicket first = businessTravelers.front();
merged.push(first);
businessTravelers.pop();
}
return merged; //HACK: dummy return
return queue<BoardingTicket>(); //HACK: dummy return
}
queue<BoardingTicket> edit(queue<BoardingTicket> &waitingQueue) {
queue<BoardingTicket> businessTravelers;
queue<BoardingTicket> economyTravelers;
split(waitingQueue, businessTravelers, economyTravelers);
return merge(businessTravelers, economyTravelers);
}
void opravilaPriLetu(queue<BoardingTicket> readyPassengers,
queue<Luggage> appliedLuggage,
Plane lt) {
if (boardingPeople.size() != 0)
izbrisiVrsto(boardingPeople);
if (unloadedLuggage.size() != 0)
izbrisiVrsto(unloadedLuggage);
boardingPeople = edit(readyPassengers);
lt.loadLuggage(appliedLuggage);
//flying to destination
unloadedLuggage = lt.unloadLuggage();
}
};
bool MergeExclusivelyInBusinessClass() {
queue<BoardingTicket> business;
int luggageID = 0;
Traveler pt1("Victor", "Candice", 25, "1111111111111");
TravelClass pr1 = BUSINESS;
Luggage k1(luggageID++);
BoardingTicket kzv1(pt1, pr1, k1);
business.push(kzv1);
Traveler pt2("Charles", "Sennet", 17, "1111111111112");
TravelClass pr2 = BUSINESS;
Luggage k2(luggageID++);
BoardingTicket kzv2(pt2, pr2, k2);
business.push(kzv2);
Traveler pt3("Marc", "Cooper", 18, "1111111111113");
TravelClass pr3 = BUSINESS;
Luggage k3(luggageID++);
BoardingTicket kzv3(pt3, pr3, k3);
business.push(kzv3);
Traveler pt4("Frank", "McLorre", 45, "1111111111114");
TravelClass pr4 = BUSINESS;
Luggage k4(luggageID++);
BoardingTicket kzv4(pt4, pr4, k4);
business.push(kzv4);
queue<BoardingTicket> economy;
queue<BoardingTicket> mergeExpected = business;
Airline ld;
queue<BoardingTicket> actuallyMerged = ld.merge(business, economy);
if (mergeExpected.size() != actuallyMerged.size()) {
cout << "Metoda merge() did not fulfill did not fullfill with correct number of passenger ("
<< (int) mergeExpected.size() << "), but with " << (int) actuallyMerged.size() << " travelers." << endl;
return false;
}
while (!mergeExpected.empty() && !actuallyMerged.empty()) {
BoardingTicket actual = actuallyMerged.front();
actuallyMerged.pop();
BoardingTicket expected = mergeExpected.front();
mergeExpected.pop();
Traveler expectedP = expected.getTraveler();
Traveler actualP = actual.getTraveler();
TravelClass expectedPR = expected.getTravelClass();
TravelClass actualyPR = actual.getTravelClass();
Luggage expectedK = expected.getLuggage();
Luggage actualyK = actual.getLuggage();
if (expectedP.getEmso() != actualP.getEmso() || expectedP.getAge() != actualP.getAge() ||
expectedP.getSurname() !=
actualP.getSurname() ||
expectedP.getName() !=
actualP.getName() || expectedPR != actualyPR ||
expectedK.getLuggageID() !=
actualyK.getLuggageID()) {
cout << "Metoda merge() did not fill up expected queue of merged travelers with travelers that were given."
<< endl;
return false;
}
}
return true;
}
int main(int argn, char **args) {
if (MergeExclusivelyInBusinessClass())
cout << "Method merge() is successful." << endl;
else
cout << "Method merge() failed." << endl;
return 0;
}
I have troubles comprehending an error, and I'm not really sure how to solve it. Any ideas?
EDIT 1
I have been notified that error could be caused by for loop itself, because queue/stack size is constantly changing, therefore it is recommended to use while loop instead.
EDIT 2
I have finally managed to discover where exactly the problems appear:
Errors_Image
Without any further information about the error, its probable the invalid pointer error is occurring in the for-loop of the loadLuggage function. The first line of the for-loop accesses the front element of the luggages queue without checking if the queue is empty. Therefore, if the variable "maxNumberOfLuggage" has been set to a value that is greater than the size of the queue, you will get an invalid pointer error. Try modifying the for-loop to:
for (int i = 0; i < this->maxNumberOfLuggage; i++) {
Luggage luggage = luggages.front();
pallet.push(luggage);
luggages.pop();
if(luggages.empty()) break; //add this line of code
}
This may solve your problem.
If not, please provide more information on the error.

Members of child class are empty (polymorphism)

I have the following problem:
I have main executable program called algatorc. In this program, I have class called TestCase, AbsAlgorithm and TestSetIterator. End user must create new algatorc program and then inherits these three classes.
Let's say that end user creates project Sorting. He would end up with three classes called SortingTestCase, SortingTestSetIterator and SortingAbsAlgorithm.
So here is the thing. End user has a method SortingTestSetIterator::get_current and return type of this function is TestCase*. In this method, he creates instance of SortingTestCase and return this instance. So he actually returned child of TestCase. In my main program I save this pointer to TestCase *t (and no, I can't save it as SortingTestCase, because before runtime, I don't know the name of the project) and then I send this pointer to method SortingAbsAlgorithm::init(TestCase* test_case). In this particulary method, end user will probably cast this object to his (SortingTestCase), and this is done like this:
sorting_test_case = dynamic_cast<SortingTestCase*>(test);
This SortingTestCase is derived from TestCase and has all members from parent class and two variables of his own: array and size. So when in init method I say
for (int i = 0; i<sorting_test_case->size; i++)
{
std::cout << sorting_test_case[i] << std::endl;
}
then I get nothing. It seems like array is empty.
Any idea what am I doing wrong?
Edit:
class SortingAbsAlgorithm : public AbsAlgorithm
{
private:
SortingTestCase *sorting_test_case;
public:
bool init (TestCase *test)
{
sorting_test_case = dynamic_cast<SortingTestCase*>(test);
std::cout << "INIT ARRAY" << std::endl;
for (int i = 0; i<sorting_test_case->size; i++)
{
std::cout << sorting_test_case->array_to_sort[i] << " ";
}
}
};
class SortingTestCase : public TestCase
{
public:
int size;
int *array_to_sort;
void init_array(int tab[], int size)
{
array_to_sort = new int[size];
for (int i = 0; i<size; i++)
{
array_to_sort[i] = tab[i];
}
}
};
class SortingTestSetIterator : public TestSetIterator
{
private:
std::string file_path;
std::string test_file_name;
public:
TestCase *get_current()
{
if (current_input_line.empty())
{
return nullptr;
}
std::vector<std::string> fields;
std::string token;
std::stringstream str(current_input_line);
while ( getline(str, token, ':') )
{
fields.push_back(token);
}
str.clear();
if (fields.size() < 3)
{
report_invalid_data_format("to few fields");
return nullptr;
}
std::string test_name = fields.at(0);
int prob_size;
try
{
prob_size = std::atoi(fields.at(1).c_str());
} catch (...)
{
report_invalid_data_format("'n' is ot a number");
}
std::string group = fields.at(2);
std::string test_id = "Test-" + std::to_string(line_number);
EParameter *test_id_par = new EParameter("TestID", "Test identificator", test_id);
EParameter *parameter1 = new EParameter("Test", "Test name", test_name);
EParameter *parameter2 = new EParameter("N", "Number of elements", std::to_string(prob_size));
EParameter *parameter3 = new EParameter("Group", "A name of a group of tests", group);
SortingTestCase *t_case = new SortingTestCase();
t_case->addParameter(*test_id_par);
t_case->addParameter(*parameter1);
t_case->addParameter(*parameter2);
t_case->addParameter(*parameter3);
int arr[prob_size];
int i = 0;
if (group == "INLINE")
{
if (fields.size() < 4)
{
report_invalid_data_format("to few fields");
return nullptr;
}
std::vector<std::string> data;
std::stringstream ss(fields.at(3));
while (getline(ss, token, ' ') )
{
data.push_back(token);
}
if (data.size() != prob_size)
{
report_invalid_data_format("invalid number of inline data");
return nullptr;
}
try
{
for (i = 0; i<prob_size; i++)
{
arr[i] = std::atoi(data.at(i).c_str());
}
} catch (...)
{
report_invalid_data_format("invalid type of inline data - data " + std::to_string((i+1)));
return nullptr;
}
}
else if (group == "RND")
{
srand(time(NULL));
for (i = 0; i<prob_size; i++)
{
arr[i] = rand() % prob_size + 1000;
}
}
else if (group == "SORTED")
{
for (i = 0; i<prob_size; i++)
{
arr[i] = i;
}
}
else if (group == "INVERSED")
{
for (i = 0; i<prob_size; i++)
{
arr[i] = prob_size - i;
}
}
t_case->init_array(arr, prob_size);
return t_case;
}
};
This are end users classes. I create library out of this classes and I load them in my program via dlopen and dlsym. I get instance of classes like this:
#ifdef __cplusplus
extern "C" {
#endif
TestSetIterator * create_iterator_object() {
return new SortingTestSetIterator;
}
void destroy_iterator_object(TestSetIterator* object) {
delete object;
}
#ifdef __cplusplus
}
and then in my main program I load symbol create_iterator_object
create_it = (TestSetIterator* (*)())dlsym(handle, "create_iterator_object");
and then I say this:
TestSetIterator *it = (TestSetIterator*)create_it();
I can call it->get_current() which loads line from file to variable curent_input_line. So I do this:
TestCase *t_case = it->get_current();
and then I say
a->init(t_case)
a is here actually SortingAbsAlgorithm loaded exactly the same as TestSetIterator (ofcourse, different symbol) and is "saved" into AbsAlgorithm a*.
So when a->init(t_case) is called, then in this init end-user cast t_case this is type of TestCase to SortingTestCase. And when I want to print elements in array, array is empty.

Convert from pointer to an object c++

I have got a problem with understanding the following code where I am trying to convert from the pointer to an object class Passenger* passenger to class Passenger passenger. I am not sure how I can modify my code to do perform the same functionality without the pointers. I am particularly confused with setting and comparing to NULL. Do I need to keep those if statements if I am no longer using the pointers? Any advises or suggestions would be appreciated.
class Seat
{
private:
class Passenger* passenger;
public:
Seat()
{
passenger = NULL;
}
~Seat()
{
if (passenger)
{
delete passenger;
passenger = NULL;
}
}
bool insertSeat(class Passenger* p)
{
bool bsuccess = TRUE;
if ( p != NULL )
{
if (passenger == NULL)
passenger = p;
else
bsuccess = FALSE;
}
else
passenger = NULL;
return bsuccess;
}
bool outputSeat(int row,int seat)
{
if (passenger)
cout << (passenger->toString()) << endl;
else
cout << "Empty " << row << seat+'A' << endl;
return passenger != NULL;
}
};
The simplest way is :
class Seat
{
private:
Passenger passenger;
public:
Seat()
{ }
void insertSeat( const Passenger& p ) // pass by const reference
{
passenger = p;
}
bool outputSeat( int row, int seat )
{
cout << passenger.toString() << endl;
}
};
You can always implement checking; eg : you don't want an invalid or default constructed passenger when passing it to
insertSeat() function :
bool insertSeat( const Passenger& p ) // pass by const reference
{
if( /* p.empty() or p.invalid() or whatever*/ ) return false; // implement empty() method
passenger = p;
return true
}

How to restrict a Class on number of objects to be created in C++?

I am just writing a sample code to create only 5 objects from a class. I have written my code like this
#include <iostream>
using namespace std;
class SingletonGeneric
{
private:
static int Count;
static SingletonGeneric *single;
SingletonGeneric()
{
//private constructor
}
public:
static SingletonGeneric* getInstance();
void method();
~SingletonGeneric()
{
Count -- ;
}
};
int SingletonGeneric::Count = 0;
SingletonGeneric* SingletonGeneric::single = NULL;
SingletonGeneric* SingletonGeneric::getInstance()
{
if( Count >= 0 && Count < 6)
{
single = new SingletonGeneric();
Count = ++;
return single;
}
else
{
return single;
}
}
void SingletonGeneric::method()
{
cout << "Method of the SingletonGeneric class" << endl;
}
int main()
{
SingletonGeneric *sc1,*sc2;
sc1 = SingletonGeneric::getInstance();
sc1->method();
sc2 = SingletonGeneric::getInstance();
sc2->method();
return 0;
}
but I am not getting the expected result. so please tell me how should I modify my code. Or if there is any other simple way to do it please let me know.
#include <iostream>
using namespace std;
class SingletonGeneric
{
private:
static int Count;
static SingletonGeneric *single;
SingletonGeneric()
{
//private constructor
}
public:
static SingletonGeneric* getInstance();
void method();
~SingletonGeneric()
{
Count -- ;
}
};
int SingletonGeneric::Count = 0;
SingletonGeneric* SingletonGeneric::single = NULL;
SingletonGeneric* SingletonGeneric::getInstance()
{
if( Count >= 0 && Count < 5) // should be 5 not 6
{
single = new SingletonGeneric();
Count++;
return single;
}
else
{
return NULL;// not the old single
}
}
void SingletonGeneric::method()
{
cout << "Method of the SingletonGeneric class" << endl;
}
int main()
{
SingletonGeneric *sc1,*sc2, *sc3, *sc4, *sc5, *sc6;
sc1 = SingletonGeneric::getInstance();
sc1->method();
sc2 = SingletonGeneric::getInstance();
sc2->method();
sc3 = SingletonGeneric::getInstance();
sc3->method();
sc4 = SingletonGeneric::getInstance();
sc4->method();
sc5 = SingletonGeneric::getInstance();
sc5->method();
sc6 = SingletonGeneric::getInstance();
if (sc6 != NULL) {
sc5->method();
} else {
cout << "only have to create 5 objects" << endl;
}
return 0;
}