Thread: Working out a data type?

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Working out a data type?

    Ok, with overloading, C++ checks the type of variable that was passes, and makes a call to that function with thoes operators.

    But outside the scope of overloading, can you figure out what type of variable something is? Say I have a function that takes a void pointer - assuming it was passed a char* array (You can do that right?);

    Code:
    void AFunction(void *AnyValue) {
     SHORT x;
    
     if(GetType?(AnyValue) == Constant_Meaning_Of_Char)
      for(x=0; x < (sizeof(AnyValue) / sizeof(GetType(AnyValue))); x++)
      cout << AnyValue[0]
     else
      cout << GetType(AnyValue);
    }
    The idea being you can find the type that was passed to the function (Even if it wouldent work in this code). Does c++ have the ability to do this? And, will this return correctly if I pass it my own object? Say;

    Code:
    class ti {
     private:
      int One;
      int Two;
     public;
      ti() {One=0; Two=0;)
      ti(int one, int two) {One=one; Two=two;}
    };
    Would the function in question return type ti?

    Thanks!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you considered using C++ templates?

    EDIT:
    Just in case you really do want to find the types, have you considered RTTI?
    Last edited by laserlight; 02-23-2006 at 04:50 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Havent heard of either of thoes :/.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Havent heard of either of thoes :/.
    You can search the Web, articles, tutorials and examples should abound.

    Actually, what problem are you really trying solve?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I have a function that takes two void pointers and attempts to cast them based on their type.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have a function that takes two void pointers and attempts to cast them based on their type.
    Why do you need to do that? Also, I think the use of void* causes the type information to be lost.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I just want to have one typecast.

    Code:
     int x;
     char y;
     string I;
    
     cast(&x, &I);
     cast(&x, &y);
    Code:
     int x;
     char y;
     string I;
    
     I=IntegerToString(x);
     y=IntegerToChar(x);
    You could overload it I supose, but it would still be nice to know if its possible to retrive a type.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can use typeid(), but in this case it looks like the use of stringstreams as per your other thread is more appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Alright, google has a nice tutorial on that, thank you again!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. how to make our own data type ?
    By Masterx in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2008, 05:04 AM
  3. return a random data type?
    By Niara in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2005, 12:58 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM