Visual c++ 如何向CString的format函数添加处理程序

Visual c++ 如何向CString的format函数添加处理程序,visual-c++,mfc,cstring,Visual C++,Mfc,Cstring,首先我有一个CString(MFC) 例如,在格式化sql字符串时 csSQL.Format( szFormat, szTableName, szColumn, szValue, intValue ) 我需要处理szValue中的特殊字符,因此我需要编写一个新类MySQLString mySQLcs.FormatSQL( szFormat, szTableName, szColumn, szValue, intValue ) 哪个有这个功能 csSQL.Format( szFormat, s

首先我有一个CString(MFC)

例如,在格式化sql字符串时

csSQL.Format( szFormat, szTableName, szColumn, szValue, intValue )
我需要处理szValue中的特殊字符,因此我需要编写一个新类MySQLString

mySQLcs.FormatSQL( szFormat, szTableName, szColumn, szValue, intValue )
哪个有这个功能

csSQL.Format( szFormat, szTableName, szColumn, HandleSpecialChar(szValue), intValue )
但是,由于参数,格式函数accept不是固定的。我觉得很难。有什么解决方案吗?

您没有向CString类添加处理程序

你写了一个(新)函数

然后进一步包装该函数(例如,
CString GetUpdateStmt(tableName,columnName,value)
),这样就不必在代码中到处散布格式字符串


在编写代码时,以结构化的方式使用SQL语句的输入,就像我建议的函数集合一样,通过使用或创建
SQLQueryStringBuilder
(化名)类,这样你就不会有在字符串格式中插入不属于该格式的内容的问题。

可能是因为我不清楚,所以我更新了帖子,因为format函数可以接受不同数量的参数,所以我不想用每个函数签名写很多FormatSQLString。再说一次:字符串/格式类是实现这一点的错误位置。请参阅答案的编辑。
csSQL.Format( szFormat, szTableName, szColumn, HandleSpecialChar(szValue), intValue )
CString FormatSQLString(CString const& format, CString const& tableName, CString const& columnName, CString const& value) {
  CString csSQL;
  return csSQL.Format(format, tableName, columnName, HandleSpecialChar(value));
}