By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,232 Members | 1,641 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,232 IT Pros & Developers. It's quick & easy.

Objects in var. length arguments (?)

P: n/a
Hello,

I have this doubt that I find no answer for:

Can an object or a reference to an object be passed as an argument to a
variable length arguments function? And if so, how does it work?

// suppose we have:
void f(int x, ...);
class A {};

// and we do:
A a;
f(3, a); // is this OK?

I recently found that MSVC++ was not complaining when I accidentally did
something like that, but I thought it was wrong. What about the standard?


Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Alvaro Segura" <as**************@vicomtech.es> wrote in message
news:bo**********@unbe.sarenet.es...
Hello,

I have this doubt that I find no answer for:

Can an object or a reference to an object be passed as an argument to a
variable length arguments function? And if so, how does it work?

// suppose we have:
void f(int x, ...);
class A {};

// and we do:
A a;
f(3, a); // is this OK?

I recently found that MSVC++ was not complaining when I accidentally did
something like that, but I thought it was wrong. What about the standard?

Your code is OK. It is the f's problem to find A object on the stack. The
common solution is to pass a kind of a format information to f.

--
Michael Kochetkov.
Jul 19 '05 #2

P: n/a

"Michael Kochetkov" <Mi***************@synartra.commmm> wrote in message news:3f******@news.telekom.ru...
Your code is OK. It is the f's problem to find A object on the stack. The
common solution is to pass a kind of a format information to f.


In this case, since A is of POD type, it is ok.
Had the class A been been a little bit more elaborate (so as to make it non-POD)
it would have been undefined behavior.
Jul 19 '05 #3

P: n/a
"Alvaro Segura" <as**************@vicomtech.es> wrote...
I have this doubt that I find no answer for:

Can an object or a reference to an object be passed as an argument to a
variable length arguments function? And if so, how does it work?
An object can, but only if the object is of POD type. A reference
probably cannot.
// suppose we have:
void f(int x, ...);
class A {};

// and we do:
A a;
f(3, a); // is this OK?
Yes, class A is a POD.
I recently found that MSVC++ was not complaining when I accidentally did
something like that, but I thought it was wrong. What about the standard?


The Standard says that if it's not POD, the behaviour is undefined.
See 5.2.2/7.

Victor
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.