|
-
Mar 13th, 2002, 10:06 AM
#1
Thread Starter
Fanatic Member
arrays of different types
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
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Mar 13th, 2002, 10:56 AM
#2
This question WAS here before, and it is not directly possible.
Why don't make a struct?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 13th, 2002, 02:54 PM
#3
Thread Starter
Fanatic Member
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
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Mar 13th, 2002, 03:00 PM
#4
Fanatic Member
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';
...
}
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Mar 13th, 2002, 03:00 PM
#5
transcendental analytic
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.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Mar 13th, 2002, 08:56 PM
#6
Thread Starter
Fanatic Member
ok, im just starting to understand polymorphism, its a cool subject but im getting a few errors and im totally lost now
Code:
#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;
}
i get these 3 errors
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)
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Mar 13th, 2002, 09:02 PM
#7
Fanatic Member
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*
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Mar 13th, 2002, 09:09 PM
#8
Thread Starter
Fanatic Member
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;
}
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Mar 13th, 2002, 11:01 PM
#9
You could also use the VARIANT data type.
Z.
-
Mar 14th, 2002, 08:40 AM
#10
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++.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 14th, 2002, 10:53 AM
#11
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.
-
Mar 14th, 2002, 12:52 PM
#12
New Member
think about collections in vb, those are string to variant maps, thats over 50 bytes per element
-
Mar 14th, 2002, 01:13 PM
#13
You really learn to hate variants when you try to use a COM object for VB in C++. Like DAO.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|