473,386 Members | 1,733 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,386 software developers and data experts.

pointer to pure virual function

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?
Nov 22 '06 #1
4 1364
Klaas Vantournhout wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 22 '06 #2
Klaas Vantournhout wrote:
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 ;-)

Nov 22 '06 #3

Klaas Vantournhout wrote:
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)
*/

Nov 22 '06 #4
Victor Bazarov wrote:
>
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)
Nov 22 '06 #5

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

Similar topics

14
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
3
by: Eric Chaves | last post by:
Hi fellows, According to the C# language specification (10.5.3), Every virtual method has a "most derived implementation" determined by a 3-step rule. If I invoke the virtual method from a normal...
3
by: Peteroid | last post by:
I'm using VS C++.NET 2005 Express in /clr. How do I create, and then use, a '^' pointer to a 'double'? That is, assuming its possible, please fill in the question marks below (there are two of...
0
by: eric | last post by:
Can I declare a pure virtual member function which accepts as input a boost shared pointer to an object of a base class, such that - concrete implementations of the function can redirect the...
6
by: MattWilson.6185 | last post by:
Hi, I'm trying to find out if something is possible, I have a few diffrent lists that I add objects to and I would like to be able to have a wrapper class that won't affect the internal object, for...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
4
emibt08
by: emibt08 | last post by:
Hi. I know the title looks a little bit silly and that we can not have pure virtual static functions. But i've been wondering what approach to take to make the abstraction. This is the case: I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...

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.