-
August 31st, 2005, 09:12 AM
#1
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 strMessage, void *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
-
August 31st, 2005, 09:20 AM
#2
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 [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
August 31st, 2005, 09:26 AM
#3
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.
-
August 31st, 2005, 09:35 AM
#4
Re: void pointer to type
Lets first analyze why this happened -
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.
-
August 31st, 2005, 09:41 AM
#5
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)
{}
-
August 31st, 2005, 09:42 AM
#6
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.
-
August 31st, 2005, 09:53 AM
#7
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.
-
August 31st, 2005, 10:08 AM
#8
Re: void pointer to type
[ moved ]
Regards,
Siddhartha
-
August 31st, 2005, 12:39 PM
#9
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
-
August 31st, 2005, 02:20 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|