CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2001
    Location
    San Diego CA
    Posts
    378

    void pointer to type

    I want to be able to use a a function like
    PHP Code:
    WriteLogType("Double Type"10.4563);
    WriteLogType("Integer Type"10);
    WriteLogType("String""Hello"); 

    etc


    I was trying to determine the type like below but it was always satisfying Integer condition. How to resolve this?

    PHP Code:
    void CMyApp::WriteLogType(CString strMessagevoid *pVoidType)
    {
        
    // then it is an integer
        
    if(sizeof(&pVoidType) == sizeof(int))
        {
            
    TRACE("Integer");
        }
        else if(
    sizeof(&pVoidType) == sizeof(float))
        {
            
    TRACE("Float");
        }
        else if((
    sizeof(&pVoidType) == sizeof(double)))
        {
            
    TRACE("Double");
        }
        else if((
    sizeof(&pVoidType) == sizeof(char)))
        {
            
    TRACE("Char");
        }
        else
        {
            
    TRACE("Unknown");
        }

    þ|êâšë rä†è rëþ|ïëš †hª† hë|þëd

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: void pointer to type

    The best way would be to create template function and \ or set of overloaded functions for logging purposes:

    Code:
    #include <iostream>
    #include <string>
    
    template <typename T>
    void WriteLog(const std::string &sMsg, T val)
    {
    	std::cout << sMsg << val << std::endl;
    } 
    
    
    
    int main(int argc, char* argv[])
    {
    	WriteLog("int:", 1);
    	WriteLog("char:", '1');
    	WriteLog("float:", 1.0f);
    	return 0;
    }
    In your example condition
    Code:
    if(sizeof(&pVoidType) == sizeof(int))
    is always true, because size of pointer is always equal to size of int.

    Hob
    Last edited by Hobson; August 31st, 2005 at 09:29 AM.
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: void pointer to type

    The template version looks the most appropriate. Just to offer a third solution, you can pass an aditional param to indicate the type.
    Code:
    enum Types {TYPE_UNKNOWN = 0, TYPE_INT, TYPE_CHAR, TYPE_STRING, ...};
    
    void CMyApp::WriteLogType(CString strMessage, void *pVoidType, int bufferSize, 
        Types type = TYPE_UNKNOWN);
    Last edited by cilu; August 31st, 2005 at 09:30 AM.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: void pointer to type

    Lets first analyze why this happened -
    Quote Originally Posted by voidspace
    I was trying to determine the type like below but it was always satisfying Integer condition.
    The sizeof (pAnyPointer) is always 4 on Win 32.

    The sizeof (int) is 4, and so is the sizeof (float) 4.
    Hence, the condition being checked was fundamentally flawed.

    You can either solve the problem using the method Hobson has recommended or using Overloaded Functions -
    Code:
    void CMyApp::WriteLog(const std::string &sMsg, int nVal)
    {
    	std::cout << sMsg << nVal << std::endl;
    };
    
    void CMyApp::WriteLog(const std::string &sMsg, float fVal)
    {
    	std::cout << sMsg << fVal << std::endl;
    };
    Last edited by Siddhartha; August 31st, 2005 at 10:09 AM.

  5. #5
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715

    Re: void pointer to type

    Or of coarse you could just overload your function.

    Code:
    main()
    {
         double myDouble            = 10.4563;  
         int        myInt                  = 10;
         char     myString[40]       = "Hello";
    
         WriteLogType(myDouble); 
         WriteLogType(myInt); 
         WriteLogType(myString); 
    }
    
    void WriteLogType(double myDouble) 
    {}
    void WriteLogType(int myInt) 
    {}
    void WriteLogType(char *pMyString) 
    {}

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: void pointer to type

    Or you could overload the function.

    void CMyApp::WriteLogType(CString strMessage, int*pIntType)
    void CMyApp::WriteLogType(CString strMessage, float*pFloatType)

    etc.

  7. #7
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: void pointer to type

    void CMyApp::WriteLogType(CString strMessage, int*pIntType)
    void CMyApp::WriteLogType(CString strMessage, float*pFloatType)
    On a side note, instead of passing a pointer (which can be invalid), one would rather pass a reference -
    Code:
    void CMyApp::WriteLog (std::string &sMsg, int & nVal)
    {
       // Implementation... 
    };
    
    void CMyApp::WriteLog (std::string &sMsg, float & fVal)
    {
       // Implementation... 
    };
    ...if the changes to passed parameter values made in WriteLog need to be reflected outside it.
    Last edited by Siddhartha; August 31st, 2005 at 10:17 AM.

  8. #8
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: void pointer to type

    [ moved ]

    Regards,
    Siddhartha

  9. #9
    Join Date
    Apr 2001
    Location
    San Diego CA
    Posts
    378

    Re: void pointer to type

    Thanks guys. I did the function overloading and it works fine.

    I like the template idea .. so will try that too.
    þ|êâšë rä†è rëþ|ïëš †hª† hë|þëd

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: void pointer to type

    Are all the functions the same? If so, maybe templates would be better. Let the compiler write out all the functions for you!



    Viggy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured