hallo,
the following program works OK:
#include <stdio.h>
struct A {
bool fun(int a, int b) {
return a == b;
}
bool (A::*ptr)(int a, int b);
A() {
ptr = &A::fun;
}
bool fun_ptr(int a, int b) {
return (A::ptr)(a, b);
}
};
main() {
A a;
printf ("%d\n", a.fun_ptr(4,4) );
printf ("%d\n", a.fun_ptr(4,3) );
}
But, after changing into STL class:
#include <stdio.h>
template <class T>
struct A {
bool fun(T a, T b) {
return a == b;
}
bool (A::*ptr)(T a, T b);
A() {
ptr = &A::fun;
}
bool fun_ptr(T a, T b) {
return (A::ptr)(a, b); // this is line 15
}
};
main() {
A<int> a;
printf ("%d\n", a.fun_ptr(4,4) );
printf ("%d\n", a.fun_ptr(4,4) );
}
during compilation the following error occures:
/home/mkwap/BGP-cvs/ccc/p11.cpp: In method `bool
A<int>::fun_ptr<int>(int, int)':
/home/mkwap/BGP-cvs/ccc/p11.cpp:21: instantiated from here
/home/mkwap/BGP-cvs/ccc/p11.cpp:15: pointer-to-member function
A<int>::ptr cannot be called
/home/mkwap/BGP-cvs/ccc/p11.cpp:15: without an object; consider
using .* or ->*
Do You know what is the problem and what to do to make second
program run
Greetings
Maciej 2 4943
"Maciej Kwapulinski" <pi****@julia.univ.gda.pl> wrote... hallo, the following program works OK:
"works OK" is not enough to be correct. It contains at least
two errors. See below.
#include <stdio.h>
struct A { bool fun(int a, int b) { return a == b; }
bool (A::*ptr)(int a, int b); A() { ptr = &A::fun; }
bool fun_ptr(int a, int b) { return (A::ptr)(a, b);
This is incorrect. Needs to be
return (this->*ptr)(a, b);
} };
main() {
This is incorrect. Needs to be
int main() {
A a; printf ("%d\n", a.fun_ptr(4,4) ); printf ("%d\n", a.fun_ptr(4,3) ); }
But, after changing into STL class:
What you change your class into is not an "STL class".
It's a class template. #include <stdio.h>
template <class T> struct A { bool fun(T a, T b) { return a == b; }
bool (A::*ptr)(T a, T b); A() { ptr = &A::fun; }
bool fun_ptr(T a, T b) { return (A::ptr)(a, b); // this is line 15
return (this->*ptr)(a, b); // this is line 15
} };
main() {
int main() {
(as you can see both changes are _precisely_ the same as in the
non-template code)
A<int> a; printf ("%d\n", a.fun_ptr(4,4) ); printf ("%d\n", a.fun_ptr(4,4) ); }
during compilation the following error occures:
/home/mkwap/BGP-cvs/ccc/p11.cpp: In method `bool A<int>::fun_ptr<int>(int, int)': /home/mkwap/BGP-cvs/ccc/p11.cpp:21: instantiated from here /home/mkwap/BGP-cvs/ccc/p11.cpp:15: pointer-to-member function A<int>::ptr cannot be called /home/mkwap/BGP-cvs/ccc/p11.cpp:15: without an object; consider using .* or ->*
Do You know what is the problem and what to do to make second program run
It will compile (and hopefully run) just fine if you make the
changes I mentioned.
Victor
Maciej Kwapulinski wrote in news:sl*******************@julia.univ.gda.pl: hallo, the following program works OK:
It shouldn't (AFAICT).
Try compiling with all warning/error's on,
for example -Wall -ansi -pedantic with g++.
If that doesn't work get a better compiler if you can.
#include <stdio.h>
struct A { bool fun(int a, int b) { return a == b; }
bool (A::*ptr)(int a, int b); A() { ptr = &A::fun; }
bool fun_ptr(int a, int b) { return (A::ptr)(a, b);
return (this->*ptr)(a, b);
This is guess. But I suspect you may think that the 'A::' you
wrote above is telling the compiler which object to call the ptr
on. If so it isn't, it's actually telling the compiler to use the
'ptr' that is in the scope of A and since fun_ptr() is a member
of A this is the default, IOW it does nothing in this context.
} };
main() { A a; printf ("%d\n", a.fun_ptr(4,4) ); printf ("%d\n", a.fun_ptr(4,3) ); }
But, after changing into STL class:
STL is a TLA that expands to [S]tandard [T]emplate [L]ibrary.
What you have here is a class template. #include <stdio.h>
template <class T> struct A { bool fun(T a, T b) { return a == b; }
bool (A::*ptr)(T a, T b); A() { ptr = &A::fun; }
bool fun_ptr(T a, T b) { return (A::ptr)(a, b); // this is line 15
same as before:
return (this->*ptr)(a, b);
} };
main() { A<int> a; printf ("%d\n", a.fun_ptr(4,4) ); printf ("%d\n", a.fun_ptr(4,4) ); }
during compilation the following error occures:
/home/mkwap/BGP-cvs/ccc/p11.cpp: In method `bool A<int>::fun_ptr<int>(int, int)': /home/mkwap/BGP-cvs/ccc/p11.cpp:21: instantiated from here /home/mkwap/BGP-cvs/ccc/p11.cpp:15: pointer-to-member function A<int>::ptr cannot be called /home/mkwap/BGP-cvs/ccc/p11.cpp:15: without an object; consider using .* or ->*
This error message actually tells you how to fix the error!
Which is nice, A shame you didn't get it for the first programme.
Do You know what is the problem and what to do to make second program run
HTH
Rob.
-- http://www.victim-prime.dsl.pipex.com/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Newsgroup - Ann |
last post by:
Gurus,
I have the following implementation of a member function:
class A
{
// ...
virtual double func(double v);
void caller(int i, int j, double (* callee)(double));
void foo() {caller(1,...
|
by: Roy Yao |
last post by:
Hello,
I need to pass a pointer to a
callback function to the lower
level modules. But the function
is thought to be a virtual member one.
How can I get the real address of
the virtual...
|
by: joe |
last post by:
hi,
after reading some articles and faq,
i want to clarify myself what's correct(conform to standard) and
what's not?
or what should be correct but it isn't simply because compilers don't...
|
by: Ben |
last post by:
Hi, there.
Recently I was working on a problem where we want to save generic
closures in a data structure (a vector). The closure should work for
any data type and any method with pre-defined...
|
by: jon wayne |
last post by:
Hi
I'm a little confused here about the lifetime of a static pointer to
member function,
Say,
I declare,define & initialize a static ptr to mem function in the
header file of a class(the class...
|
by: dice |
last post by:
Hi,
In order to use an external api call that requires a function pointer I
am currently creating static wrappers to call my objects functions.
I want to re-jig this so I only need 1 static...
|
by: JohnQ |
last post by:
The implementation of classes with virtual functions is conceptually easy to
understand: they use vtables. Which begs the question about POD structs: how
are they associated with their member...
|
by: WaterWalk |
last post by:
Hello. I thought I understood member function pointers, but in fact I
don't. Consider the following example:
class Base
{
public:
virtual ~Base() {}
};
class Derived : public Base
{
|
by: Tim Frink |
last post by:
Hi,
I'm experimenting with function pointers and found
two questions. Let's assume this code:
1 #include <iostream>
2 class A;
3
4 ////////////////////////////////////////////
5 class B
|
by: Immortal_Nephi |
last post by:
I had a lot of research to see how function pointer works. Sometimes,
programmers choose switch keyword and function in each case block can
be called. Sometimes, they choose ordinary function...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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: 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,...
|
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,...
|
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...
| |