how to create a class from an identifier? - c++

the title might be a bit misleading, but I want to give some instance of a class an instance of a different class through polymorphism. I think that may have been even more misleading so I'll give an example;
Say I have a class called Spell, that is a parent class to the class Firebolt. I want another class, say Character, to be able to have the spell, 'Firebolt', in an its memory without ever having to #include the files for 'Firebolt'.
Now, I can think of a way that prior games have done this before. By giving each spell (or whatever else specific class type) a static const string ID or name and in spell having some function that can access this ID/name and return a new Firebolt() if they are the same.
This sounds pretty good actually, the problem I'm having is I don't know how to code this. I'm not sure how I can access these ID's from the parent class, and I'm not sure how to make a virtual function that will actually return the correct Spell. Any help would be amazing. I'll also provide the actual class code I'm working with here in case it might help you guys to answer, or someone with a similar problem to solve it.
The parent class;
class Art {
std::string name;
int EPCost;
int castTime;
int AoESize;
public:
Art(std::string n, int cp, int ct, int as):name(n), EPCost(cp), castTime(ct), AoESize(as) {}
virtual ~Art() {}
static Art* findArt(std::string ID);
int getAoESize() {return AoESize;}
std::string getName() {return name;}
int getEPCost() {return EPCost;}
virtual int getBaseDamage() = 0;
};
The subclass;
class FireBolt:public Art {
static const std::string name;
static const int EPCost;
static const int castTime;
static const int AoESize;
static const std::string ID;
public:
FireBolt():Art(name, EPCost, castTime, AoESize) {}
~FireBolt() {}
int getBaseDamage();
};

All you need to do is make your FireBolt::ID public.
class FireBolt:public Art {
public:
static const std::string ID;
...
};
Art* Art::findArt(const std::string& ID)
{
if (ID == FireBolt::ID)
return new FireBolt(...);
...
}

Related

how typeid(type).name() decide name for user define class? and can i change this behaviour?

i want to know for user define class how typeid(type) will decide the name for type of user define class check my below code for student class i got an output like 7Student but i don't understand why 7 is appended before Student class.
#include <iostream>
class Person {
protected:
std::string name;
public:
Person(std::string name)
: name(name) {}
};
class Student : public Person{
private:
std::string id;
public:
Student(std::string name,std::string id)
: id(id) , Person(name) {}
};
template<typename Type>
class Test {
private:
Type type;
public:
Test(Type type)
: type(type) {}
const char* getType() const {
return typeid(this->type).name();
}
};
int main() {
Test<int> *test1 = new Test<int>(5);
std::cout<<test1->getType()<<std::endl;
Test<float> *test2 = new Test<float>(1.1);
std::cout<<test2->getType()<<std::endl;
Test<double> *test3 = new Test<double>(1.1);
std::cout<<test3->getType()<<std::endl;
Test<long> *test4 = new Test<long>(11);
std::cout<<test4->getType()<<std::endl;
Test<unsigned int> *test5 = new Test<unsigned int>(11);
std::cout<<test5->getType()<<std::endl;
Test<Student> *test6 = new Test<Student>(*(new Student("visrut","111")));
std::cout<<test6->getType()<<std::endl; // 7Student
Test<Person> *test7 = new Test<Person>(*(new Person("visrut")));
std::cout<<test7->getType()<<std::endl;
Test<std::string> *test8 = new Test<std::string>("visrut");
std::cout<<test8->getType()<<std::endl;
return 0;
}
i tried this code with extend Person class and without extend Person class but output is same for last type and it is 7Student.
for your reference on my g++ compiler output is below
i
f
d
l
j
7Student
6Person
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Now when i tested for Person class also it's output is 6Person so i found this behaviour as {length of user-define class}{class-name} but i got weird output for std::string so i want to ask can i change this behaviour in Person or Student class is there is something built in method i can write in Person or Student class that return specific const char * for typeid().name() ?
The name() member of std::type_info is totally implementation defined. You cannot change what is output, and it is not known what will be output. It may even be different between runs.
Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program.
From here.
If you need to define and retrieve a specific name from a class, you are looking at doing something called reflection (or a very simply form of it). This is not something that c++ was designed for and you should avoid relying on needing to do this if you can. IF you find you really need to do it, you can find more info at How can I add reflection to a C++ application?.

Pointers disappear after calling sub-routine of child class

I am trying to do One class with some features that will be em common with other (Inheritance). This is my principal class:
class SREngineJulius_father {
protected:
bool g2pInit;
Recog *recog;
Jconf *jconf;
public:
bool InitG2P(std::string dic, std::string model, int pos_process_flag=-1);
bool Init_AM(std::string configStr);
};
The two function InitG2P and Init_AM, will make updates in recog and jconf. This updates is something that all child objects of the child class must have.
class SREngineJulius: public SREngineJulius_father
{
DFA_INFO *dfaInfo;
WORD_INFO *wordInfo;
WORD_INFO *wordInfo_init;
Output lastResult;
TListString lastCmdList;
bool startNotifyCallbackLoop;
bool terminate;
bool pause;
DFA_INFO* copy_dfa_info(DFA_INFO* dfa);
DFA_INFO* create_commands_dfa_info();
static void status_recready(Recog *recog, void *dummy);
static void status_recstart(Recog *recog, void *dummy);
static void output_result(Recog *recog, void *dummy);
static void put_hypo_phoneme(WORD_ID *seq, int n, WORD_INFO *winfo);
std::string ReplaceSpace(std::string &str);
std::string RestoreSpace(std::string &str);
public:
bool InitG2P(std::string dic, std::string model, int pos_process_flag=-1);
char* NotifyCallbackLoop(char *ficheiro_wav);//AXY5
int SREngineJulius::Audio_Buffering(char* buffer_audio, int sizefile, int end_flag_, int flag_alocation);//AXY5
void Callbacks();
public:
~SREngineJulius();
bool InitSREngine(std::string recoConfig);
bool DynamicAddCommands(TListString &cmdlist, int startRecog = -1);
bool DynamicAddCommands(std::string cmdlist, std::string sep=" ", int startRecog = -1);
void Release();
};
So the problem is, when I call a routine of the child class, recog and jconf are deleted.
You should make functions in your class that edify the two variables. Try inserting those into a anonymous struct. That way they're easily accessible.
Remember:
. =reference
-> =pointer
Lastly, you are changing recog in a couple of those function declarations.
Try to make sure that the functions are friendly in the functions you make. If they are not, you won' be able to use them.
P.s. children of child classes, these need accessor functions. They have to change the pointers through upward inheritance. So you would want to have the child class of the grandparent have a class that changes it's pointers. You would do that by having a function which accesses the child, in the grandchild. That way you're receding back into the function. That's how it's related.

Gather data from loosely coupled classes

In a software project, I've done my best to make the classes loosely coupled. Now I'm struggling to get the scattered data (from objects of the loosely coupled classes) into a data object. This data object (e.g. for logging) is predefined and has to be filled with data from objects of several classes.
As the real code is proprietary, I've written the code below which should illustrate my problem:
class Gear {
public:
Gear(unsigned int amount);
~Gear();
void shiftUp();
void shiftDown();
private:
const unsigned int amount;
unsigned int current;
};
class Speedometer {
public:
Speedometer();
~Speedometer();
void display() const;
private:
double currentSpeed;
};
class Bike {
public:
Bike(unsigned int nrWheels, Gear& gear, Speedometer& speedometer);
~Bike();
void peddle() const;
void break() const;
private:
const unsigned int nrWheels;
Gear& gear
Speedometer& speedometer;
};
class Cyclist {
public:
Cyclist(const std::string name, Bike& bike);
~Cyclist();
void ride();
private:
const std::string name;
Bike& bike;
};
class Factory {
public:
Factory();
~Factory();
Cyclist& build() const;
};
struct PredefinedData {
PredefinedData();
~PredefinedData();
std::string cyclistName;
unsigned int nrWheels;
unsigned int currentGear;
double currentSpeed;
};
A Factory (object) builds a Cyclist (object) with all objects that it references (recursively). The state of all these objects will change during the execution of the program. In order to e.g. log the state of a Cyclist object with all its references, an object from the PredefinedData class is used. This object has to be filled with data from Cyclist and its references.
Some thoughts/tests of my own:
I've tried giving every class a reference of the PredefinedData object, but that meant that private data of objects would be known by other objects.
I've tried a Filler object that provides functions to fill the PredefinedData object, but it feels like duplicating the PredefinedData class.
I've also considered inheriting a class from several classes to be able to give every class access to just the data it should have access to/fill.
How would you solve this? Is there a design pattern for this situation?
I'd do this:
class Cyclist {
public:
Cyclist(const std::string name, Bike& bike)
: data(name), bike(bike) {}
const PredefinedData& getPredefinedData()
{
data.nrWheels = bike.getNrWheels();
data.currentGear = bike.gear.getCurrent();
data.currentSpeed = bike.speedometer.getSpeed();
return data;
}
private:
PredefinedData data; // includes name, used as a cache
Bike& bike;
};

C++ polymorphic functions with differing return types

I am creating a property class which stores a unique key and an arbitrary value as strings (plus an optional comment string for use when writing to configuration files). Currently I'm using the method of creating a base property class which holds the raw strings, and then subclassing this into type-specific properties - eg. an IntProperty which implements a getValue() function that converts the string to an int - to avoid having to convert a property value manually from a string every time I want to read it. These subclasses use getPropertyType(), a virtual function defined in the base and overridden in each of the derived, to return an enum value to identify which type of property they hold, and the base class returns a "none" identifier.
(As a side note, I shied away from templates because I'm using Qt and its required interface macro doesn't support templated objects. If it's worth using templates I may ditch the idea of using interfaces.)
My intention was to allow for lists of multiple different types of properties (string, int, float...) by subclassing them from the base property class and allowing arrays of base property pointers. However, I run into the problem that it then becomes very awkward to extract the property as a specific type from one of the derived classes, since the pointer to the base class obviously does not know about the newly defined getValue functions in the derived classes. I am left with either the option of extracting the string from the base class and converting manually or by casting the base class pointer to the correct derived class pointer. The first option renders the subclassing useless by requiring that I do the conversion manually, and the second sounds like a nightmare to code since there'll be a large switch statement involved on the property identifier value each time I want to work out which pointer to cast to.
What would be the most intelligent way of going about this problem? I want to keep the retrieval of property values as simple as possible - ie. have as little boilerplate code as I can to go from getting a base class pointer from an array to holding a properly typed copy of the property's value. Would it be worth considering the problem the other way around - have multiple strongly-typed property classes which all support getting and setting their respective value using a string?
What about this? (Untested, but you should get the idea)
class BaseType {
public:
virtual void getValue(string &s) { s = ""; };
virtual void getValue(int &i) { i = 0; };
virtual void getValue(double &d) { d = 0.0; };
};
class IntType : public BaseType {
public:
virtual void getValue(string &s) { s = to_string(myvalue); };
virtual void getValue(int &i) { i = myvalue; };
virtual void getValue(double &d) { d = static_cast<double>(myvalue); };
private:
int myvalue;
};
class DblType : public BaseType {
public:
virtual void getValue(string &s) { s = to_string(myvalue); };
virtual void getValue(int &i) { i = static_cast<int>myvalue; };
virtual void getValue(double &d) { d = myvalue; };
private:
double myvalue;
};
class StrType : public BaseType {
public:
virtual void getValue(string &s) { s = myvalue; };
virtual void getValue(int &i) { i = stoi(myvalue); };
virtual void getValue(double &d) { d = stod(myvalue); };
private:
string myvalue;
};
Surely, since the receiving side needs to know what type it's getting, using a name that indicates what you get back, e.g.
int GetInt(const string& key);
string GetString(const string& key);
double GetDouble(const string& key);
etc. would be just as good as calling it Get(const string& key) - and since the C++ language doesn't allow you to ONLY differentiate on the return type, that wouldn't work.
Another alternative is of course to have a
template <typename T>
void Get(const string& key, T& value);
(May need to actually implement all the different variant's differently, so it may not really help much to use a template, but it's much easier for me to write in an answer as a template! ;) )

C++ Design pattern for separate accessor/mutator interfaces

I have a data structure "Person"
struct Person
{
protected:
string name;
int age;
string address;
...
}
I want to create "views" around this structure to separate out access to different member variables:
class PersonRead: public Person
{
public:
string getName() {..}
int getAge() {...}
...
}
class PersonUpdate: public Person
{
public:
void setAddress( string address_ ) {...}
void setAge( int age_ ) {...}
...
}
I use this to only expose those methods/variables which are really required:
int main()
{
...
writePersonDataToFile ( (PersonRead) personObj );
updatePersonData ( (PersonUpdate) personObj);
...
}
Though the above code serves my purpose, there are several issues including:
The public inheritence here is not exactly an 'is-a' relationship
I need to derive IndianPerson from Person, and all the corresponding interfaces. This leads to bad diamond pattern:
struct IndianPerson: public Person {};
class IndianPersonRead: public IndianPerson, public PersonRead {}; //Person Class common, Diamond pattern here!
Is there a name for such a design pattern? What are better ways to implement this pattern? I have a feeling Policy classes might help, but cant figure out how to implement this
Any examples would be great help
For your scenario this might seem like overkill but, if you want fine grained control over which classes can call different methods on your class the c++ client-attorney idiom idiom might be appropriate.
For a detailed description of this idiom see http://drdobbs.com/184402053
Here is a rough example (note: this has not been compiled, although it is based on production code I am currently using):
class Person
{
public:
/// constructor destructor etc:
private:
string getName() { return name; }
public:
/// Writer Attourney that access to allows class PersonReader access
/// to getXXX functions
class ReaderAttorney
{
private:
/// Add additional reader member functions...
static string readName( Person& p )
{
return p.getName();
}
// Make any classes that shuold be allowde read access friends of the
// attorney here
friend class PersonReader;
};
/// Writer Attourney that access to allows class PersonWriter access
/// to setXXX functions
class WriterAttorney
{
private:
/// Add additiona reader member functions...
static string setName( Person& p, const string& newName )
{
p.setName( newName );
}
friend class PersonWriter;
};
private:
string name;
int age;
string address;
};
This can be used as follows:
void PersonWriter::setPersonDetails( const string& name, int age .... )
{
// PersonWriter is a frend of WriterAttorney and is granted access
Person::WriterAttorney::setName( name );
Person::WriterAttorney::setName( age );
// Note this will fail, since PersonWriter is not a friend of
// ReaderAttorney, ergo it is not granted read permission:
Person::ReaderAttorney::readName();
}
I think that your approach is not correct at all: PersonRead and PersonUpdate are not Persons. They read and modify Person data but are not really Person.
In the same way, IndianPersonRead and IndianPersonUpdate are not an IndianPerson.
I separate this relationship in following:
PersonRead use Person
PersonUpdate use Person
IndianPerson inherits from Person: is a Person
IndianPersonRead inherits from PersonRead and use IndianPerson
IndianPersonUpdate inherits from PersonUpdate and use IndianPerson
I show an example of my apporach:
#include <string>
#include <iostream>
using namespace std;
struct Person
{
string getname() const { return name; }
string getaddress() const { return address; }
void setaddress(const string & address_) { address = address_; }
void setname(const string & name_) { name = name_; }
protected:
string name;
int age;
string address;
};
class PersonRead
{
public:
string getname(const Person & p) { return p.getname(); }
};
class PersonUpdate
{
public:
void setAddress(Person & p, const string & address_ ) {p.setaddress(address_); }
void setname(Person & p, const string & name_ ) {p.setname(name_); }
};
struct IndianPerson : public Person
{
string gettribe() const { return tribe; }
void settribe(const string & tribe_) { tribe = tribe_; }
protected:
string tribe;
};
struct IndianPersonRead : public PersonRead
{
public:
string gettribe(const IndianPerson & p) const { return p.gettribe(); }
};
struct IndianPersonUpdate : public PersonUpdate
{
public:
void settribe(IndianPerson & p, const string & t) { p.settribe(t); }
};
int main(int argc, char **argv)
{
IndianPerson ip;
IndianPersonUpdate ipU;
IndianPersonRead ipR;
ipU.settribe(ip, "Cheroki");
ipU.setname(ip, "Charly");
cout << ipR.getname(ip) << " : " << ipR.gettribe(ip) << endl;
}
First of all I will agree with the Tio's point of view PersonUpdate is not a Person so there is a wrong inheritance usage. Also I believe that you need to make your classes with target to represent the real world so classes like PersonUpdate are wrong because they represent the action and not the object.
In your case one solution could be to use the visitor design pattern, so the Person could accept a especially designed IPersonStream interface in order to perform the serialization in classes which will implement this interface.
Person stream will accept the persons attributes on it or for good the Person's memento take a look on memento design pattern, and serialize it to xml or whatever you want.
I don't have a design pattern name, but to resolve your concerns, I would swap the inheritance relation and let Person inherit from the PersonReader and PersonWriter interfaces. This way, objects that must only read from Person use the PersonReader interface and as such promises to not change it.
By making every member of Person private, you can even make sure that Person is not accessed in another way, but then every class that inherits from Person should have these members private.