Working out a data type?

This is a discussion on Working out a data type? within the C++ Programming forums, part of the General Programming Boards category; Ok, with overloading, C++ checks the type of variable that was passes, and makes a call to that function with ...

  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
    22,304
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,304
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,304
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,304
    You can use typeid(), but in this case it looks like the use of stringstreams as per your other thread is more appropriate.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09: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, 01:58 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 05: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, 04:00 PM

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