Thread: program variables

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    program variables

    Hello..

    Im working on a class that would store different variables for my application..

    Code:
    class datalist {
    public:
    	datalist() {}
    
    	char *name;
    	unsigned short type;
    	void *p;
    	datalist *next;
    };
    It would be possible to store char * (string), char ** (string list), and integer.
    type 0 = integer, 1 = string, 2-> = string list (the number of strings)

    p is the pointer to data.

    Is it smart doing things like that?

    I want a function that would return string (if char*) or first string (if char**)

    Code:
    int readdata(datalist *read) {
    	char *value = 0;
    	if (read && read->type) {
    		if (read->type > 1) 
    			value = (char **)(read)->p; // <- problem
    		else
    			value = (char *)(read)->p;
    
    		printf("value: %s\n", value);
    	}
    	return 0;
    }
    The code is not okay, I get error if I cast (char**)(read)->p[0] - "'void *' : unknown size".. Im not sure how should I do it? Is it smart doing things that way?
    Last edited by l2u; 08-27-2006 at 10:14 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is it smart doing things like that?
    I would suggest you look up how to do templates, which allow you to create instances of your class suited to whatever data type(s) you're interested in storing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Also you do't need to store an abstract representation of types. You can use typeid().

    Code:
    int x = 0;
    double y = 0;
    int* ptr = &x;
    int z = 0;
    
    if( typeid(int) == typeid(x) ) std::cout << "Match!\n"; else std::cout << "No Match!\n";
    if( typeid(int) == typeid(y) ) std::cout << "Match!\n"; else std::cout << "No Match!\n";
    if( typeid(int*) == typeid(ptr) ) std::cout << "Match!\n"; else std::cout << "No Match!\n";
    
    //or by comparing objects directly...
    
    if( typeid(x) == typeid(z) ) std::cout << "Match!\n"; else std::cout << "No Match!\n";
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Salem
    > Is it smart doing things like that?
    I would suggest you look up how to do templates, which allow you to create instances of your class suited to whatever data type(s) you're interested in storing.
    Im not used to templates, and I would prefer to do it that way.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Well, were you used to classes before you practiced them or learned them? I think not. So all salem was doing was giving you one of the better, and easier ways of doing what you want. Now if you know classes templates are not but a baby step further. I would also suggest learning templates as they make life as a programmer much easier in the long run

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you don't know how to create templated code, you could just use existing templated code that already has implemented what you are trying to achieve. Look up boost::variant. Your variant can hold an int, string, or vector<string>. You can store these variants in an std::list or std::vector. All these tools are already made, tested and debugged and are a much smarter option than attempting to implement this yourself. That comment includes the use of string instead of char* and std::list or std::vector (or some other container) instead of your own liked list implementation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-07-2009, 09:07 AM
  2. Editing variables while running a flash program.
    By two31d in forum Tech Board
    Replies: 2
    Last Post: 02-28-2006, 01:27 PM
  3. Parse a program for functions, variables
    By Enu in forum C Programming
    Replies: 2
    Last Post: 02-15-2006, 11:08 AM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM