473,804 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointers to member functions and default parameters

Hello all,

At the line marked "Problem here???" below, I get successful compilation on
one platform and failure on another. What does the Standard say about this?
Is this a correct program? May a member function be called through a
pointer when some parameter default values are accepted (i.e. not all
parameters are passed)?

Thanks,
Dave
#include <iostream>

using namespace std;

class foo
{
public:
int do_it(int = 10) {return 42;}
};

template <typename RET, typename OBJECT_TYPE, typename PTR_TO_MEM_FUN>
RET call_it(OBJECT_ TYPE &ds, PTR_TO_MEM_FUN pm)
{
return (ds.*pm)(); // Problem here???
}

int main()
{
foo bar;

cout << call_it<int>(ba r, &foo::do_it) << endl;
}
Jul 22 '05 #1
2 1470
Dave <be***********@ yahoo.com> spoke thus:
#include <iostream> using namespace std; class foo
{
public:
int do_it(int = 10) {return 42;}
}; template <typename RET, typename OBJECT_TYPE, typename PTR_TO_MEM_FUN>
RET call_it(OBJECT_ TYPE &ds, PTR_TO_MEM_FUN pm)
{
return (ds.*pm)(); // Problem here??? ^^^^ I don't think this is legal :)
} int main()
{
foo bar; cout << call_it<int>(ba r, &foo::do_it) << endl;
}


(I'm not an authority; any of the below could be [really?] wrong!)

Well, among other things, you didn't complete the template
specification (I'm sure that's the wrong word...) - the template takes
three arguments, and you only supplied one. Another thing is that
template arguments should be object types, and a pointer to a member
function doesn't sound like it belongs there.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #2

"Christophe r Benson-Manica" <at***@nospam.c yberspace.org> wrote in message
news:c6******** **@chessie.cirr .com...
Dave <be***********@ yahoo.com> spoke thus:
#include <iostream>

using namespace std;

class foo
{
public:
int do_it(int = 10) {return 42;}
};

template <typename RET, typename OBJECT_TYPE, typename PTR_TO_MEM_FUN>
RET call_it(OBJECT_ TYPE &ds, PTR_TO_MEM_FUN pm)
{
return (ds.*pm)(); // Problem here???

^^^^ I don't think this is legal :)
}

int main()
{
foo bar;

cout << call_it<int>(ba r, &foo::do_it) << endl;
}


(I'm not an authority; any of the below could be [really?] wrong!)

Well, among other things, you didn't complete the template
specification (I'm sure that's the wrong word...) - the template takes
three arguments, and you only supplied one. Another thing is that
template arguments should be object types, and a pointer to a member
function doesn't sound like it belongs there.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.


The other two template parameters are deduced by the compiler.

A template type parameter does not have to be of class type. It may be of a
built-in type, an enumeration, a pointer-to-member, etc...
Jul 22 '05 #3

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

Similar topics

26
2473
by: Desmond Liu | last post by:
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use of references when passing parameters. I understand the reasoning behind using references--you avoid the cost of creating a local object when you pass an object by reference. But why use a reference? Is there any inherent advantage of using a reference over using a pointer? My workplace discourages the use of pointers when it comes to parameter passing. They...
2
3285
by: David Williams | last post by:
Hi Guys, I have a couple of problems. The first is (I believe) a simple syntactic problem you can probably solve quite easily. The second is a design style problem which might be more tricky... So, I have a class with a couple of integers. I provide a couple of functions to operate on those integers (add, subtract) and the function to be called is chosen via a function pointer. Like so: 01: #include <iostream> 02:
3
6702
by: Bilgehan.Balban | last post by:
Hi, How do I declare an array of function pointers and assign each element of the array with public member functions of a class? Is it possible that the array is not a member of the class? Thanks, Bahadir
3
3660
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
5
3184
by: Martijn van Buul | last post by:
Hi. I'm having a peculiar problem at work. I've been googling for it, but haven't found an authorative answer. In a nutshell (Long story follows), what I'd like to know is: If I have a C++ class which has several virtual functions and several member variables, will the default copy constructor also copy the virtual table pointer, or will it merely do a (shallow) copy of the member variables? I'm using gcc 4.1.1
18
2884
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to write an ordinary differential equation class that would solve a general equation in the form: dx/dt = f(x,t). The ode class shouldn't know anything about f, except how to call it.
12
7874
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
16
328
by: Wabz | last post by:
I am trying to use pointers to create a payroll program. I've used various functions to do the process, but I seem to be having problems in calling the functions appropriately so each pointer knows where to read its data from. Here's what I have so far...can someone help me figure out what am doing wrong? Many thanks. #include <ctype.h> #include <stdlib.h>
2
1890
by: Eric Lilja | last post by:
As the topic says, I wanted to make a re-usable singleton class that could create pointers to objects with non-trivial constructors. I came up with this: #ifndef SINGLETON_HPP #define SINGLETON_HPP template<typename T> struct DefaultCreatorFunctor {
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.