John Gateley
2007-10-24 18:55:20 UTC
Hi.
I'm having trouble with the dreaded:
cannot pass objects of non-POD type 'sometype' through '...'
message. Here's a brief example:
class String {
public:
void SetData(char *NewData) { m_Data = NewData; }
char *m_Data;
};
int Bar(char *s, va_list ArgList)
{
printf("String: %s\n", s);
printf("Arg2: %s\n", va_arg(ArgList, char *));
}
int Foo(char *s, ...)
{
va_list ArgList;
va_start(ArgList, s);
int Result = Bar(s, ArgList);
va_end(ArgList);
return Result;
}
int main(int argc, char **argv)
{
String MyString;
MyString.SetData("ghi");
Foo("abc", MyString);
}
This works because String::m_Data is public. If I only change
it so that m_Data is private:
class String {
public:
void SetData(char *NewData) { m_Data = NewData; }
private:
char *m_Data;
};
The program now gives the dreaded warning, and crashes on execution.
Given that I know what I am doing (I am using the fact that String
has a single data member which is a pointer to character to copy just
that pointer to character to the argument list), is there any way to
accomplish this?
Is the crash a "real" crash, or is it a crash generated by the compiler
just to enforce passing non-PODness? It definitely seems like the latter.
The situation is this: I have a lot of code that uses a string class
which takes advantage of the pun: the string class has only the
one data member, which means you can do things like printf("%s", obj)
and have the right thing happen (using a different compiler, of course).
Is there any way to use this useful pun with g++?
Thanks,
j
I'm having trouble with the dreaded:
cannot pass objects of non-POD type 'sometype' through '...'
message. Here's a brief example:
class String {
public:
void SetData(char *NewData) { m_Data = NewData; }
char *m_Data;
};
int Bar(char *s, va_list ArgList)
{
printf("String: %s\n", s);
printf("Arg2: %s\n", va_arg(ArgList, char *));
}
int Foo(char *s, ...)
{
va_list ArgList;
va_start(ArgList, s);
int Result = Bar(s, ArgList);
va_end(ArgList);
return Result;
}
int main(int argc, char **argv)
{
String MyString;
MyString.SetData("ghi");
Foo("abc", MyString);
}
This works because String::m_Data is public. If I only change
it so that m_Data is private:
class String {
public:
void SetData(char *NewData) { m_Data = NewData; }
private:
char *m_Data;
};
The program now gives the dreaded warning, and crashes on execution.
Given that I know what I am doing (I am using the fact that String
has a single data member which is a pointer to character to copy just
that pointer to character to the argument list), is there any way to
accomplish this?
Is the crash a "real" crash, or is it a crash generated by the compiler
just to enforce passing non-PODness? It definitely seems like the latter.
The situation is this: I have a lot of code that uses a string class
which takes advantage of the pun: the string class has only the
one data member, which means you can do things like printf("%s", obj)
and have the right thing happen (using a different compiler, of course).
Is there any way to use this useful pun with g++?
Thanks,
j
--
John Gateley <***@jriver.com>
John Gateley <***@jriver.com>