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 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
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 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
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
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
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.
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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)...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |