Create va_list dynamically

2020-02-01 00:14发布

I have a function

void foo(int cnt, va_list ap);

I need to use it, but requirement is quite strict, number of va_list vary and it will change during run-time. What I would like to do is:

create a va_list (which expects char*) form

QList<Contact*>

where Contact is a defined class

class Contact
{
   public:
      QString getName();
   private: 
      QString m_name;

}; 

and I would like to populate in the loop va_list for example:

for (int idx = 0; idx<contacts.count(); idx++)
{
    contacts.at(idx)->getName(); // this i would like to pass to va_list

}

Does anybody have a clue about how I could do this?

7条回答
啃猪蹄的小仙女
2楼-- · 2020-02-01 01:16

Your question is tagged C++ and there are nice ways (like streams) to avoid varargs completely in C++.

This is a great example of why va_args can cause pain. If you have any chance at all to change the signature of foo, that's your best option. Taking a std::vector<std::string> instead of va_list would just solve your problem right there.

If foo is in an external library you can't change, my next suggestion would be to find a different library.

If none of those is an option it seems like there ought to be a way to recursively build up the call list using va_list, but I couldn't figure out how to make that work.

查看更多
登录 后发表回答