Quick Answer...

This is a discussion on Quick Answer... within the C++ Programming forums, part of the General Programming Boards category; I have a pointer.... is there a way to chagne what data type it is? Like i have 5 structures ...

  1. #1
    Unregistered
    Guest

    Quick Answer...

    I have a pointer.... is there a way to chagne what data type it is?

    Like i have 5 structures and i need to use the same variable but be able to switch betwen which type it is in the program. I dont really want to just cast it to another one cuz it get all bloated code. but can i just do someting to delete the current pointer and make it again w/ a different data type?

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    You could declare a void pointer and then make it point to the appropriate type.

    I'm not exactly sure if this is what you want.
    Eg.
    Code:
    typedef struct{/*...*/}ONE;
    typedef struct{/*...*/}TWO;
    typedef struct{/*...*/}THREE;
    typedef struct{/*...*/}FOUR;
    typedef struct{/*...*/}FIVE;
    
    int main()
    {
    	void *ptr;
    	ptr = new ONE;
    	delete ptr;
    	ptr = new TWO;
    	delete ptr;
    	ptr = new THREE;
    	delete ptr;
    	ptr = new FOUR;
    	delete ptr;
    	ptr = new FIVE;
    	delete ptr;
    	
    	return 0;
    }

  3. #3
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    You could always redefine the 5 structures as 5 classes that derive from a common base class. A pointer to a class can be assigned to derived classes as well.

    Void pointers are handy for passing addresses, but you have to typecast them each time you use them.
    "Logic is the art of going wrong with confidence."
    Morris Kline

  4. #4
    Unregistered
    Guest
    OK, I understand your problem now.

    If you used that method then you'd have to cast the pointer each time you used it.
    Code:
    int main()
    {
    	void *ptr;
    	ptr = new ONE;
    	((ONE*)ptr)->foo = NULL;
    	delete ptr;
    	return 0;
    }
    You could define a structure that had pointers to the other structures as members.
    Code:
    typedef struct{char* foo;}ONE;
    typedef struct{char* foo;}TWO;
    typedef struct{char* foo;}THREE;
    typedef struct{char* foo;}FOUR;
    typedef struct{char* foo;}FIVE;
    
    typedef struct
    {
    	ONE* one;
    	TWO* two;
    	THREE* three;
    	FOUR* four;
    	FIVE* five;
    }ALL;
    
    int main()
    {
    	ALL* ptr = new ALL;
    	
    	ptr->one = new ONE;
    	ptr->one->foo    = NULL;
                    delete ptr->one;
                    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. quick question
    By SenorJesucristo in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2006, 09:06 PM
  3. Quick question, should be simple to answer.
    By kabuatama in forum C Programming
    Replies: 4
    Last Post: 01-21-2006, 02:42 PM
  4. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  5. getting weird answer when not using ".h"
    By CobraCC in forum C++ Programming
    Replies: 10
    Last Post: 05-07-2003, 06:21 AM

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