pointer to pure virual function

K

Klaas Vantournhout

Hi,

I try to set a function pointer to a pure virtual functions in the base
class itself.

To shorten, assume the following code

class base {
public:
base() { base::Fptr = &base::F;}
void (*Fptr) (int);
virtual void F(int) = 0;
};

compiling it gives me

[~]$ g++ -c virtualptr.cpp
virtualptr.cpp: In constructor ‘base::base()’:
virtualptr.cpp:3: error: cannot convert ‘void (base::*)(int)’ to ‘void
(*)(int)’ in assignment

Where am I going wrong, and more important.
Is it actually possible?
 
K

Klaas Vantournhout

Klaas said:
Hi,

I try to set a function pointer to a pure virtual functions in the base
class itself.

To shorten, assume the following code

class base {
public:
base() { base::Fptr = &base::F;}
void (*Fptr) (int);
virtual void F(int) = 0;
};

compiling it gives me

[~]$ g++ -c virtualptr.cpp
virtualptr.cpp: In constructor ‘base::base()’:
virtualptr.cpp:3: error: cannot convert ‘void (base::*)(int)’ to ‘void
(*)(int)’ in assignment

Where am I going wrong, and more important.
Is it actually possible?

Okay compilation is successful when implementing the following

class base {
public:
base() { base::Fptr = &base::F;}
void (base::*Fptr) (int);
virtual void F(int) = 0;
};

never second-guess the compiler ;-)
 
V

Victor Bazarov

Klaas said:
I try to set a function pointer to a pure virtual functions in the
base class itself.

To shorten, assume the following code

class base {
public:
base() { base::Fptr = &base::F;}
void (*Fptr) (int);

You need to declare Fptr as

void (base::*Fptr)(int);
virtual void F(int) = 0;
};

compiling it gives me

[~]$ g++ -c virtualptr.cpp
virtualptr.cpp: In constructor 'base::base()':
virtualptr.cpp:3: error: cannot convert 'void (base::*)(int)' to 'void
(*)(int)' in assignment

Where am I going wrong, and more important.

Read the error message.
Is it actually possible?

Should be possible. What's the point, though? You can't call it.

V
 
S

Salt_Peter

Klaas said:
Hi,

I try to set a function pointer to a pure virtual functions in the base
class itself.

To shorten, assume the following code

class base {
public:
base() { base::Fptr = &base::F;}
void (*Fptr) (int);
virtual void F(int) = 0;
};

compiling it gives me

[~]$ g++ -c virtualptr.cpp
virtualptr.cpp: In constructor 'base::base()':
virtualptr.cpp:3: error: cannot convert 'void (base::*)(int)' to 'void
(*)(int)' in assignment

Where am I going wrong, and more important.
Is it actually possible?

Yes, but what for since you can't use it. Note that nothing stops from
calling the pure virtual from derived.

#include <iostream>

class base {
public:
base() { }
virtual void F(int) = 0;
};

void base::F(int n) { std::cout << "base::F(int)\n"; }

class derived : public base
{
void F(int n)
{
std::cout << "derived::F(int)\n";
base::F(n);
}
};

int main()
{
derived d;
base* p_base = &d;
p_base->F(0);
}

/*
derived::F(int)
base::F(int)
*/
 
P

Pete Becker

Victor said:
Should be possible. What's the point, though? You can't call it.

You can call it. It's just an ordinary virtual call:

#include <iostream>
struct Base
{
virtual void f() = 0;
virtual ~Base() {}
};

struct Derived : public Base
{
void f() { std::cout << "Here I am!\n"; }
};

int main()
{
void (Base::*fp)() = &Base::f;
Base *bp = new Derived;
(bp->*fp)();
delete bp;
return 0;
}

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top