Results 1 to 13 of 13

Thread: arrays of different types

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    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.

  4. #4
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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?

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    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.

  7. #7
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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?

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    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.

  9. #9
    Zaei
    Guest
    You could also use the VARIANT data type.

    Z.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  11. #11
    Zaei
    Guest
    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.

  12. #12
    New Member star's Avatar
    Join Date
    Mar 2001
    Location
    In the distant future...
    Posts
    9
    think about collections in vb, those are string to variant maps, thats over 50 bytes per element
    It's astronomical!

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width