converting

This is a discussion on converting within the C++ Programming forums, part of the General Programming Boards category; Is there any way to change the template of an object? Such that vector <int> list(10); can be converted to ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    converting

    Is there any way to change the template of an object?

    Such that

    vector <int> list(10);

    can be converted to

    vector <char> list(10);

    and stored in the same vector?

    Or is there a way to fake it?

    possibly using typename?
    Last edited by Trauts; 04-09-2003 at 12:21 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    Couple of possibilities come to mind, depending on if you want to store multiple data types in one object or create a separate object for each data type.

    If you want an object for each data type, create a template using "template <class T>"

    If you are trying to store multiple data types in one object, you are most likely going to have to convert to strings to go into the object, then convert back to whichever data type you started with to use it. That seems pretty difficult to me.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I want to make the datatype the template represents different.

    Basically, is there a way to make something like

    Code:
    template <class a>
    class datatype
    {
        public:
               a data;
    };
    Where you declare one like this:

    datatype<char> name;

    What I want to do is make THE SAME variable correspond to an object with type

    datatype<int>

    so that I can store an unknown datatype, but then later make the same variable store a different type.

    In other words, I'd like to be able to do something like this:

    Code:
    #include <iostream.h>
    
    template <class a>
    class datatype
    {
        public:
               a data;
    };
    
    int main()
    {
    	datatype *name;
    	name = new datatype<int>;
    	name.data = 50;
    	cout << name.data << endl;   //should be 50
    	delete name;
    	name = new datatype<char>;
    	name.data = 'a';
    	cout << name.data << endl;  //should be 'a'
    	delete name;
    	return 0;
    }
    Last edited by Trauts; 04-09-2003 at 09:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tips for converting C prog. to C++?
    By eccles in forum C++ Programming
    Replies: 7
    Last Post: 01-15-2005, 06:38 AM
  2. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  3. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  4. Converting from infix to postfix
    By jcramer in forum C Programming
    Replies: 4
    Last Post: 03-27-2004, 08:23 PM
  5. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21