473,383 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

default arg value lost via function pointer

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
May 22 '06 #1
9 3089

Metaosp wrote:
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

May 22 '06 #2
On Mon, 2006-05-22 at 03:41 -0700, Sunil Varma wrote:

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
May 22 '06 #3
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

May 22 '06 #4
On Mon, 2006-05-22 at 04:04 -0700, Sunil Varma wrote:
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
May 22 '06 #5
Metaosp wrote:
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
May 22 '06 #6
Metaosp wrote:
On Mon, 2006-05-22 at 04:04 -0700, Sunil Varma wrote:
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?


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

May 22 '06 #7
On Mon, 2006-05-22 at 13:57 +0200, Rolf Magnus wrote:
Metaosp wrote:
On Mon, 2006-05-22 at 04:04 -0700, Sunil Varma wrote:
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?


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


So this is a defined (or induced) limitation of C++ function pointers?
Metaosp
May 22 '06 #8
Metaosp wrote:
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

May 22 '06 #9
Metaosp wrote:
On Mon, 2006-05-22 at 13:57 +0200, Rolf Magnus wrote:
Metaosp wrote:
> On Mon, 2006-05-22 at 04:04 -0700, Sunil Varma wrote:
>> 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?


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


So this is a defined (or induced) limitation of C++ function pointers?


Yes.

May 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jacob | last post by:
I just wrote a function that draws a fractal by recursively calling itself, and ran into an odd (odd from my point of view anyway) problem. I've managed to solve (or rather, circumnavigate) the...
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
3
by: Ed Morton | last post by:
Anyone know why "const" isn't the default for function parameters? I almost never see code where function parameters are modified intentionally (and when I do, I usually think copying the value to...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
7
by: prefersgolfing | last post by:
In 6, by default arguments, were passed byref what is the default in .NET? Can you point me to any MSDN documentation? TIA
5
by: asianmuscle | last post by:
I am trying to learn RAII and some template techniques by writer a smarter pointer class to manage the resource management. Since I find that a lot of the resource management is kinda the same, I...
10
by: Brad Baker | last post by:
I have an asp.net/csharp application that requires a particular variable to work properly. This variable is usually passed via a query string in the URL when the application is first run but under...
10
by: sam.barker0 | last post by:
Hi there, I am trying to develop a compressed suffix trie. But first I want to get the trie working. The structure of the trie is like class trie { private: bool last_node; map<char,...
6
tpgames
by: tpgames | last post by:
I'm trying to get a maze to default to always giving me the image as the marker and never give me the color as the marker. The code requires you to check a box to get the image marker to be used. So,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.