The paragraph below was extracted from page 420 from Stroustup book "The C++ Programming Language" (third edition):
Because a pointer to a virtual member (s in this example) is a kind of offset, it does not depend on an object’s location in memory. A pointer to a virtual member can therefore safely be passed between different address spaces as long as the same object layout is used in both. Like pointers to ordinary functions, pointers to nonvirtual member functions cannot be exchanged between address spaces.
I'm disputing the last sentence in this paragraph. Below, you'll find a code snippet where pointers to nonvirtual member functions, foo()
and foo1()
, are exchanged between one base object a
and a derived object b
, without a problem.
What cannot be done is the overloading, of any of the functions in the base, foo()
or foo1()
, in the derived class, for in this case the compiler will emit an error as shown below.
#include <iostream>
class A
{
int i;
public:
A() : i(1) {}
void foo() { std::cout << i << '\n'; }
void foo1() { std::cout << 2 * i << '\n'; }
};
class B: public A
{
int j;
public:
B() : A(), j(2) {}
// void foo() { std::cout << j << '\n'; }
};
int main()
{
typedef void (A::* PMF)();
PMF p = &B::foo; // error C2374: 'p' redefinition, multiple initialization
// if foo() is overloaded in B.
PMF q = &B::foo1;
B b;
(b.*p)();
(b.*q)();
A a;
(a.*p)();
(a.*q)();
}
“I miss” is something that you want or crave. “I am missing” is something that belongs there.
The sentence, “I'm missing you” is entirely correct and natural. It would be equally correct to say, “I miss you.”
"I'm missing someone" This sentence is in present continuous tense therefore here it means that you're missing someone at that particular moment. This sentence is generally used when you're having a conversation, so as to show your present state. 2. "I miss someone" This sentence means you you miss someone often.
That sentence is correct: In (standard) C++, a program, or rather process, has exactly one address space. So as ulidtko pointed out, this sentence is referring to the possibilities of exchanging pointers to virtual vs. non-virtual member functions between address spaces of different processes.
A non-virtual member function of a class is pretty much a standard function with an implicit argument to the object you call it for (the this pointer). As such, it gets assigned some address in your process's address space on loading. Where exactly it ends up in your address space is certainly depending on your platform and whether or not that member function is part of a dynamically linked library. The point is, for two processes it is not necessarily the same address. So passing a pointer to and then executing such a function in another process will potentially 'set your machine on fire (TM)'.
A virtual member function is still pretty much the same as the non-virtual member function as in 'some address in memory you jump to on execution and pass it your this pointer', but it gets called through the virtual function table (vtable) instead of directly. So a pointer to a virtual member function is pretty much just an index into your object's virtual function table. Calling that function does then something along the lines of 'taking your object pointer, maybe increment the pointer to get to the object's vtable and jump to the address at the given index of that table, passing the address of the object itself as the this pointer'. So this indirection via the vtable makes exchanging the pointer to the virtual member function between address spaces work.
Disclaimer: I am leaning somewhat over my "I really know what I am talking about"-comfort zone here. So in case I oversimplified something or worse, yet, engaged in distributing false information, feel free to shred my answer apart ;).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With