Casting with character arrays

This is a discussion on Casting with character arrays within the C++ Programming forums, part of the General Programming Boards category; Code: template <typename OldType, typename NewType> any & change_type(any & operand) { try { any::placeholder * newcontent = new any::holder<NewType>(void_cast<NewType>(any_cast<OldType>(operand))); ...

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

    Templates and pointers

    Code:
    	template <typename OldType, typename NewType>
    	any & change_type(any & operand)
    	{
    		try
    		{
    		any::placeholder * newcontent = new any::holder<NewType>(void_cast<NewType>(any_cast<OldType>(operand)));
    		std::swap(operand.content,newcontent);
    		delete newcontent;
    		}
    		catch (std::bad_cast)
    		{
    			cout << "TYPE1: " << typeid(OldType).name() << ' ' << "TYPE2: " << typeid(NewType).name() << endl;
    			cout << "BOO HOO" << endl;
    			return any::any();
    		}
    		cout << "TESTING" << endl;
    		return operand;
    	}
    Using the catch, I finally figured out that the problem is that the any_cast isn't working. I then found out that the templates are not reading something like

    change_type<int*,char*>(x);

    as that, but as

    change_type<int,char>(x);

    which causes it to crash, because any_cast makes sure it returns the type that is currently stored in the any object.

    Why isn't it taking the pointer?
    Last edited by Trauts; 05-02-2003 at 08:09 PM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is function template overloading.

    template <typename OldType, typename NewType>
    any *& change_type(any *& operand)

    template <typename OldType, typename NewType>
    any * change_type(any * operand)

    Kuphryn

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I thought I tried that and it gave me problems... I'll try again

    thx

    Edit: Yeah, it didn't work. It can't convert any to any*...

    That wasn't the problem though

    The template seems to be discarding the pointer information, (int* is read as int), so its basically useless... If you try using a pointer, the program crashes.

    Its basically the difference between

    changetype<int*,char*>(x);

    any::placeholder * newcontent = new any::holder<char*>(void_cast<char*>(any_cast<int*> (operand)));

    and

    changetype<int,char>(x);

    any::placeholder * newcontent = new any::holder<char>(void_cast<char>(any_cast<int>(op erand)));

    It never tries using char* or int*... it reads them as char and int, so it crashes with pointers.

    I can't figure out why the templates aren't keeping all of the data!
    Last edited by Trauts; 05-02-2003 at 10:10 PM.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    It works if I change it to

    Code:
    	template <typename OldType, typename NewType>
    	any & change_type(any & operand, OldType oldz, NewType newz)
    	{
    		try
    		{
    		any::placeholder * newcontent = new any::holder<NewType>(void_cast<NewType>(any_cast<OldType>(operand)));
    		// any::placeholder * newcontent = new any::holder<NewType>(static_cast<NewType>(any_cast<OldType>(operand)));
    		std::swap(operand.content,newcontent);
    		delete newcontent;
    		}
    		catch (std::bad_cast)
    		{
    			cout << "TYPE1: " << typeid(OldType).name() << ' ' << "TYPE2: " << typeid(NewType).name() << endl;
    			cout << "BAD CAST" << endl;
    		}
    		return operand;
    	}
    But that defeats the purpose because its terribly inconvenient to do this:

    change_type(x,(int*)1,(char*)1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. scanf(), Parameter Passing, and Character Arrays
    By linguae in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 05:19 AM
  5. help with character arrays
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-28-2002, 06:13 PM

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