is it possible to make an array where every element is a different data type?
i think i saw this question asked before but i can't find it now
Printable View
is it possible to make an array where every element is a different data type?
i think i saw this question asked before but i can't find it now
This question WAS here before, and it is not directly possible.
Why don't make a struct?
how about getting the data type of a variable and using that to declare another variable?
something like
typedef typeof(myVar) Var;
Var newVariable;
where you don't know what type myVar is but you need to create another variable of the same type
but i dont think that solves his question
a struct would be easier.
Code:struct mystruct{
int num1;
char letter;
}
void main(){
mystruct[2];
...
mystruct[0].num1=5;
mystruct[1].letter='a';
...
}
its quite possible, trough polymorphism, you store either a pointer to a commonly inherited base object, or make a base that stores the delegated object on the heap.
ok, im just starting to understand polymorphism, its a cool subject but im getting a few errors and im totally lost now
i get these 3 errorsCode:#include<iostream>
#include<vector>
using namespace std;
/////////////////////////////////////////////////////
class MenuBase{};
/////////////////////////////////////////////////////
template<class InputType, class ReturnType, class ParamType1, class ParamType2>
class MenuItem : public MenuBase
{
public:
InputType GetInput(void){return m_Input;}
void SetInput(InputType Input){m_Input = Input;}
private:
InputType m_Input;
};
/////////////////////////////////////////////////////
class Menu
{
public:
Menu(vector<MenuBase*> items)
:m_items(items)
{}
int GetSize(void){return m_items.size();}
private:
vector<MenuBase*> m_items;
};
/////////////////////////////////////////////////////
int main()
{
vector<MenuBase*> menu(3);
MenuItem<int, int, char, int> menuitem1;
menuitem1.SetInput(100);
menu[0] = menuitem1;
MenuItem<char, int, int, long> menuitem2;
menuitem2.SetInput('A');
menu[1] = menuitem2;
MenuItem<bool, bool, int, char> menuitem3;
menuitem3.SetInput(true);
menu[2] = menuitem3;
Menu theMenu(menu);
cout << theMenu.GetSize() << endl;
return 0;
}
C:\Dan's Stuff\School\C++ Projects\CS4\Polymorphism\TempMenu.cpp(46) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class MenuItem<int,int,char,int>' (or there is no acceptable conversion)
C:\Dan's Stuff\School\C++ Projects\CS4\Polymorphism\TempMenu.cpp(50) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class MenuItem<char,int,int,long>' (or there is no acceptable conversion)
C:\Dan's Stuff\School\C++ Projects\CS4\Polymorphism\TempMenu.cpp(54) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class MenuItem<bool,bool,int,char>' (or there is no acceptable conversion)
i got that error on a program and i fixed it by adding a .cpp file into my project that wasnt added.
it was like
blah.h
blah.cpp
i included the .h file, and not the cpp file in the project. i just added the cpp file and it worked.
that prolly not the case here. polymorphism...hmmm *looks for tutorials*
phew, i haven't run it yet, but i got it to compile with zero errors
i just realized i had read the part on inheritance and polymorphism at http://members.tripod.com/~firstpod/cpp21/
but i had forgotten that you need to assign pointers to derived classes to the base class pointer, not the actual derived object and then it was telling me that the variables weren't initialized so i added the = new... stuff
Code:int main()
{
vector<MenuBase*> menu(3);
MenuItem<int, int, char, int>* menuitem1 = new MenuItem<int, int, char, int>;
menuitem1->SetInput(100);
menu[0] = menuitem1;
MenuItem<char, int, int, long>* menuitem2 = new MenuItem<char, int, int, long>;
menuitem2->SetInput('A');
menu[1] = menuitem2;
MenuItem<bool, bool, int, char>* menuitem3 = new MenuItem<bool, bool, int, char>;
menuitem3->SetInput(true);
menu[2] = menuitem3;
Menu theMenu(menu);
cout << theMenu.GetSize() << endl;
return 0;
}
You could also use the VARIANT data type.
Z.
Variants suck (who spends 20 bytes on a single variable? It it were 16 then at least alignement would be ok, but this way it actually uses 32 bytes!).
If you do want to use them, you're better off using the _variant_t wrapper class defined in comutil.h. This is only available in MSVC++.
I know varients suck =). You could create your own, though, if you know what types of variables that you are going to be using =).
Z.
think about collections in vb, those are string to variant maps, thats over 50 bytes per element
You really learn to hate variants when you try to use a COM object for VB in C++. Like DAO.