Dynamic class object load [duplicate] - c++

This question already has answers here:
Saving a game state using serialization C++
(3 answers)
Serialization without Boost.Serialization
(3 answers)
Is it possible to serialize and deserialize a class in C++?
(14 answers)
Closed 2 years ago.
Is there a way to use something like std::is_base_of based on the strings generated by typedid(...).name(), and not on the types themselves?
In extenso, given something like:
class A {...};
class B: public A {...};
A a;
B b;
std::string sa = typeid(a).name;
std::string sb = typeid(b).name;
I would need something like this, at runtime:
if( std::is_base_of(sa, sb) )
{...}
This is related to object serialization in files.
Is there a way to perform this (without boost)?

Related

c++ Circular dependency with the need to know the class attributes [duplicate]

This question already has answers here:
C++ class declaration after using it
(3 answers)
What are forward declarations in C++?
(8 answers)
How to create two classes in C++ which use each other as data?
(2 answers)
recursive definition in CPP [duplicate]
(5 answers)
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 7 months ago.
First of all, I think this problem has already been discussed and is pretty common but id doesn't seem to find an answer appropriate to my problem.
My problem is related to circular dependencies, which by itself can be solved by using forward declaration, however, in my case, I need to know more
then the class name in the forward declaration (like attributes and functions).
In my code both classes, Time and Distance/Speed need to know each other and each other attributes. I avoided the problem by declaring an "Interface" of Time which contains the attribute that is needed for Distance/Speed, but I'm wondering if there is a more elegant solution to this problem that wouldn't involve creating an "Interface".
class ITime
{
public:
ITime(float s) :time_s(s) {};
float time_s;
};
class Speed
{
public:
Speed(float ms = 0) :speed_ms(ms) {}
float speed_ms;
};
class Distance
{
public:
Distance(float m = 0) :distance_m(m) {}
float distance_m;
Speed operator/ (const ITime& t) const
{
return Speed(distance_m / t.time_s);
};
};
class Time :public ITime
{
public:
Time(float s):ITime(s) {}
Distance operator *(const Speed& speed) const
{
return Distance(time_s * speed.speed_ms);
}
};

Changing class to struct [duplicate]

This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 8 months ago.
I wonder if I can directly convert the public class to a struct. Is it permissible?
class car
{
public:
int vno;
float count;
char dname[15],x,l[50];
void input()
Is it okay just to convert to struct directly like this?
struct car
{
int vno;
float count;
char dname[15],x,l[50];
void input()
will it be run with no error? Or there is another way...
A class has private fields by default, with no keywords such as "private" and "public". If you add these attributes, you can manipulate fields' visibilities.
A struct, on the other hand, has public fields by default and we do not have to add keywords of the type public; but we can also have private data in the structure after the private keyword.

Finding the class of an instance of a class for a dynamic_cast [duplicate]

This question already has answers here:
C++ equivalent of java's instanceof
(5 answers)
C++: using typeinfo to test class inheritance
(3 answers)
Closed 5 years ago.
I would like to know if it is possible to do something like the "instanceof" of Java in C++.
Indeed I searched a lot but I have not found anything for finding the class of an instance of a class.
To sum up, I would like to use something like this:
bool instanceOf(Gui guiA, Gui guiB)
{
Gui *castedGuiPtr = dynamic_cast<classOf(guiA)*>(guiB);
return castedGuiPtr != 0;
}

Pointer to a logger class provide for all other classes? [duplicate]

This question already has answers here:
Error logging in c++
(3 answers)
Closed 7 years ago.
I have a Logger class in my C++ application. This class has public methods like writeDebug(std::string & str) , can write to a debug output file and it works for me very good. The object is being created on application start by the first main class and there it is stored as a Logger * myLogger; object.
Since I have other classes as well, I would like to provide this logging functionality to those classes as well.
Currently I oberhead the myLogger pointer to toher classes in their contructor, and all the other classes store this very same Logger pointer when being created --> all other classes has this Logger * myLogger pointer stored:
Other_XY_Class::Other_XY_Class(Logger * logger, ...)
: localLoggerPtr{logger} { //.. }
==> localLoggerPtr (in class Other_XY_Class) == myLogger (in main application class)
I can imagine that there is a more convenient / elegant way to handle that ?
Since you only have one instance, why not use a Singleton for that?
Something along this lines:
class foo {
private:
void bar();
static foo Instance;
public:
static void Bar() { Instance.bar(); }
}

How to use an array within a class? [duplicate]

This question already has answers here:
Unresolved external symbol on static class members
(6 answers)
Closed 7 years ago.
I'm currently designing some code on QT that is built across multiple source. I want to create an array in one source and be able to access it in another source.
Currently in my Header I have class
Array_Class : public QString
{
public:
static QString Data_Array [2];
};
I don't think I need a constructor as I'm going to "populate" the array before I read it.
currently in my source.cpp I have
Array_Class::Data_Array[0]= "foo";
Array_Class::Data_Array[1]= "bar";
however this gives me the error message undefined reference to "Array_Class::Data_Array". What am I missing? thanks
So far, you have only declared your array:
Array_Class : public QString
{
public:
static QString Data_Array [2]; // -> only a declaration!
};
In order to use it, you must now define it. To do this, you need to place somewhere in your .cpp:
QString Array_Class::Data_Array [2];