How does a variable arguement function take object to struct/class? For example :
CString a_csName;
CString a_csAge(_T("20"));
a_csName.Format(_T("My Age is : %s"),a_csAge);
Here CString::Format
is a printf-style variable arguement function which takes this CString
object. How is this possible?
After a bit of researching and debugging through MFC code found the below, hope it helps whoever faces the ever famous static code analyser error "Passing struct 'CStringT' to ellipsis" which actually is the source of my doubt as well.
http://www.gimpel.com/html/bugs/bug437.htm
Format function being a variadic function, depends on the format specifier present in the first parameter. First parameter is always a char *.
It parses for the format specifier(%s,%d,%i…) and read the var_arg array based on the index of the format specifier found and does a direct cast to char * if %s or int if %d is specified.
So if a CString is specified and corresponding format specifier is %s, then a direct cast attempt to char * is made on the CString object.
CString a_csName;
CString a_csAge(_T("20"));
a_csName.Format(_T("My Age is : %s"),a_csAge);
wcout<<a_csName;
Would print My Age is 20
CString a_csName;
CString a_csAge(_T("20"));
a_csName.Format(_T("My Age is : %d"),a_csAge);
wcout<<a_csName;
Would print My Age is 052134
So there is no intelligence behind this. Just a direct cast. Hence we pass a POD or User-Defined Data structure it doesn’t make a difference.