default arg value lost via function pointer

M

Metaosp

Hi,

How can I call a function through function pointer while keeping the
default argument value working? I mean:

void foo(int i=3) {
cout << i << endl;
}

void (*bar)(int);

int main() {
bar = &foo;

foo(); // ok, prints '3'
bar(1); // ok, prints '1'

bar(); // doesn't compile...
// default argument lost via function pointer
}



Thanks,
Metaosp
 
S

Sunil Varma

Metaosp said:
Hi,

How can I call a function through function pointer while keeping the
default argument value working? I mean:

void foo(int i=3) {
cout << i << endl;
}

void (*bar)(int);

int main() {
bar = &foo;

foo(); // ok, prints '3'
bar(1); // ok, prints '1'

bar(); // doesn't compile...
// default argument lost via function pointer
}



Thanks,
Metaosp

Please change your declaration of function pointer to

void (*bar)(int = 3);

now you can call the bar with or without pointer.

As per the declation of the bar function pointer, the compiler assumes
that the pointer will be called with one int argument.

The compiler doesnt what will be stored in bar, whose value is
asssigned runtime.

Regards
Sunil Varma
 
M

Metaosp

Please change your declaration of function pointer to

void (*bar)(int = 3);

now you can call the bar with or without pointer.

I've tried this, but it doesn't compile... the error message I got is
"default arguments are only permitted for function parameters". Any
idea?


Thanks,
Metaosp
 
S

Sunil Varma

May I know the compiler you are using?

I tried in gcc 3.4.2 and Borand C++ 5.5.1.

It worked fine.

Regards
Sunil Varma
 
J

Joe Van Dyk

Metaosp said:
Hi,

How can I call a function through function pointer while keeping the
default argument value working? I mean:

void foo(int i=3) {
cout << i << endl;
}

void (*bar)(int);

int main() {
bar = &foo;

foo(); // ok, prints '3'
bar(1); // ok, prints '1'

bar(); // doesn't compile...
// default argument lost via function pointer
}

I don't believe you can. Default arguments aren't considered part of a
function's type.

I'm suprised this isn't a FAQ -- I can't find it at
http://www.parashift.com/c++-faq-lite.

Joe
 
M

Metaosp

May I know the compiler you are using?

I tried in gcc 3.4.2 and Borand C++ 5.5.1.

It worked fine.

Interesting, I am using gcc 4.0.2. I wonder whether this is an
undefined behavior of C++ or a bug in gcc 4?


Metaosp
 
R

Rolf Magnus

Metaosp said:
Interesting, I am using gcc 4.0.2. I wonder whether this is an
undefined behavior of C++ or a bug in gcc 4?

It's a bug in gcc 3.4.2 and Borland C++ 5.5.1.
 
J

Jonathan Mcdougall

Metaosp said:
How can I call a function through function pointer while keeping the
default argument value working? I mean:

This is explicitly forbidden by the standard :

"This means that default arguments cannot appear, for example, in
declarations of pointers to functions, references to functions, or
typedef declarations." (8.3.6§3 note 88)
void foo(int i=3) {
cout << i << endl;
}

void (*bar)(int);

int main() {
bar = &foo;

foo(); // ok, prints '3'
bar(1); // ok, prints '1'

bar(); // doesn't compile...
// default argument lost via function pointer
}


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top