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

avoiding virtual call with pointer to member function

Hi to all,
a newbie question. Is it possible to avoid the virtual mechanism with
pointers to members syntax the same way it's done with an explicit
call ( obj.Base::Func() )? It would be useful for something I'm
doing... check out the trivial program down here...
Thanks in advance,
Francesco

#include <iostream>

struct Base
{
virtual ~Base() {}
virtual void Do() const { std::cout << "Base\n"; }
};

struct Der : public Base
{
virtual void Do() const { std::cout << "Der\n"; }
};

int main()
{
Der obj;

obj.Base::Do(); // avoid virtual call

void ( Base::*ptrToMembFunc )() const = &Base::Do;

( obj.*ptrToMembFunc )(); // virtual call
// is there a way to avoid virtual call with pointers to member
functions
// as in the above direct call?
}

Apr 4 '07 #1
3 1794
xt*********@gmail.com wrote:
Hi to all,
a newbie question. Is it possible to avoid the virtual mechanism with
pointers to members syntax the same way it's done with an explicit
call ( obj.Base::Func() )? It would be useful for something I'm
doing... check out the trivial program down here...
Thanks in advance,
Francesco

#include <iostream>

struct Base
{
virtual ~Base() {}
virtual void Do() const { std::cout << "Base\n"; }
};

struct Der : public Base
{
virtual void Do() const { std::cout << "Der\n"; }
};

int main()
{
Der obj;

obj.Base::Do(); // avoid virtual call

void ( Base::*ptrToMembFunc )() const = &Base::Do;

( obj.*ptrToMembFunc )(); // virtual call
// is there a way to avoid virtual call with pointers to member
functions
// as in the above direct call?
}
You could try forcing 'obj' into being a 'Base' by means of
'static_cast'...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 4 '07 #2
Thanks for the reply, but doesn't work. I (guess) the vptr points
always to the same vtable.
Tried below
#include <iostream>

struct Base
{
virtual ~Base() {}
virtual void Do() const { std::cout << "Base\n"; }
};

struct Der : public Base
{
virtual void Do() const { std::cout << "Der\n"; }
};

int main()
{
Der obj;

obj.Base::Do(); // avoid virtual call

void ( Base::*ptrToMembFunc )() const = &Base::Do;

( obj.*ptrToMembFunc )(); // virtual call
// is there a way to avoid virtual call with pointers to member
functions
// as in the above direct call?

( static_cast< Base & >( obj ).*ptrToMembFunc )();
// won't work, the vptr always points to the same vtable

}

On Apr 4, 11:14 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
xtrigger...@gmail.com wrote:
Hi to all,
a newbie question. Is it possible to avoid the virtual mechanism with
pointers to members syntax the same way it's done with an explicit
call ( obj.Base::Func() )? It would be useful for something I'm
doing... check out the trivial program down here...
Thanks in advance,
Francesco
#include <iostream>
struct Base
{
virtual ~Base() {}
virtual void Do() const { std::cout << "Base\n"; }
};
struct Der : public Base
{
virtual void Do() const { std::cout << "Der\n"; }
};
int main()
{
Der obj;
obj.Base::Do(); // avoid virtual call
void ( Base::*ptrToMembFunc )() const = &Base::Do;
( obj.*ptrToMembFunc )(); // virtual call
// is there a way to avoid virtual call with pointers to member
functions
// as in the above direct call?
}

You could try forcing 'obj' into being a 'Base' by means of
'static_cast'...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Apr 4 '07 #3
On Apr 4, 5:25 pm, xtrigger...@gmail.com wrote:
Thanks for the reply, but doesn't work. I (guess) the vptr points
always to the same vtable.
// top posting is not recommended

#include <iostream>

struct Base
{
virtual ~Base() {}
virtual void Do() const { std::cout << "Base::Do()\n"; }
void foo() const
{
std::cout << "Base::foo()\n";
Base::Do();
}
};

struct Der : public Base
{
virtual void Do() const { std::cout << "Der::Do()\n"; }
};

int main()
{
Der obj;
obj.Base::Do();

void ( Base::*ptrToMembFunc )() const = &Base::foo;
( obj.*ptrToMembFunc )();
}

/*
Base::Do()
Base::foo()
Base::Do()
*/

Apr 4 '07 #4

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

Similar topics

3
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...
4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
3
by: Daniel Graifer | last post by:
Why doesn't c++ support virtual data? It supports class data of type pointer to function (that's what virtual functions really are). In terms of implementation, why can't I have other types of...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
12
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
9
by: ypjofficial | last post by:
Hello All, I am defining a class with one virtual function and storing its first 4 bytes ie. the address of the virtual function table to a file.I am again rereading the file in the same program...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
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 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.