I want my void to allow multiple struct definitions from a single argument lets say thats "myStructAny" but I can put there myStructA and myStructB. Is that possible ?
no matter what type it is, type is accessible from any of them as the first element, due to alignment etc, so you can get the type and then handle it internally.
there are other ways to do the same thing -- eg basically taking an axe to the problem and treating the structs as byte soup. So yes, it is possible. Should you do this? Probably not. Its a mad hax answer to a 'can you do it' question --- which begs "what do you really want to accomplish that can't be done with inheritance or polymorphism?" Because this works, but its ugly, and its not a good way to do anything.
Can you please explain why something like mbozzi's solution doesn't work for you? Simple function overloading looks like it will do everything you need. Your structs have completely different data in them, and you are not showing how they are related. There are many different designs that could possibly apply here, but it's hard to tell what you actually want. You might also want to consider constexpr if or template specialization, to at least avoid the unnecessary runtime checks.
That last code uses all the bad parts of dynamic behavior and inheritance and none of the good parts. Your Render functions don't even do anything now. And what's up with the test() function?
Instead of using typeid/dynamic_cast, use virtual functions and polymorphism. It would at least be better than what you have now.
If your structs had something common between them, you could also do something like:
//works like enum but type is not enum
class Align : MyBaseClass
{
public:
staticconstunsignedshortint Middle=1;
staticconstunsignedshortint Top = 2;
staticconstunsignedshortint Left = 3;
staticconstunsignedshortint Bottom = 4;
staticconstunsignedshortint Right = 5;
};
class Orientation : MyBaseClass
{
public:
unsignedshortint Vertical = Align::Bottom;
unsignedshortint Horizontal = Align::Bottom;
Orientation(unsignedshortint, unsignedshortint);
};
/*SAME DEFINITIONS IN PREVIOUS*/
Orientation::Orientation(unsignedshortint _Vertical, unsignedshortint _Horizontal) : Vertical(_Vertical), Horizontal(_Horizontal) {};
void resultToString(unsignedshortint align) {
switch (align)
{
case Align::Bottom: cout << "\nBottom"; break; case Align::Left: cout << "\nLeft"; break; case Align::Top: cout << "\nTop"; break; case Align::Right: cout << "\nRight"; break;
default:
break;
}
}
int main()
{
auto testStruct1 = Render(MyStructA(192)); //struct
auto testStruct2 = Render(MyStructB("this is a test string"));//struct
//not struct type is Orientation which has static "const unsigned short int" members
Orientation* testStruct3 = Render(&Orientation(Align::Top, Align::Left));
/*if(testStruct1...) ... if (testStruct2...)…*/
if (typeid(*testStruct3) == typeid(Orientation))
{
Orientation* ori = dynamic_cast<Orientation*>(testStruct3);
resultToString(ori->Horizontal);
resultToString(ori->Vertical);
}
/*…*/
}
OUT:
1 2 3 4
192
this is a test string
Horizontal->Left
Vertical->Top
I do not insist I should do it like this I can be wrong.
I still don't quite understand what's happening, but a few thoughts...
(1) &Orientation(Align::Top, Align::Left)
is taking the address of a temporary object, which doesn't even compile for me.
main.cpp: In function ‘int main()’:
main.cpp:31:75: error: taking address of temporary [-fpermissive]
Orientation* testStruct3 = Render(&Orientation(Align::Top, Align::Left));
(2)
1 2 3 4
class MyBaseClass {
public:
MyStructBase();
...
The name of what I assume is supposed to be your default constructor does not match your class name.
(3) As you mentioned, you are using your Align class as if it were just an enum. Why have it inherit from MyBaseClass if it has nothing to do with it?
You could just use an enum class instead,
1 2 3 4 5 6 7 8
enumclass Align : short
{
Middle = 1,
Top = 2,
Left = 3,
Bottom = 4,
Right = 5
};
And pass in objects of type Align, not short.
(I also suggest just using int instead of short, unless you have a good reason for using shorts everywhere)