Hi,
Is there ever a situation where the programmer needs to be concerned about the copying of a vtable pointer or can I happily assume that the C++ language will handle that detail correctly without my intervention?
Hi,
Is there ever a situation where the programmer needs to be concerned about the copying of a vtable pointer or can I happily assume that the C++ language will handle that detail correctly without my intervention?
I have never found a need to copy the vtable in any of my c++ programs. I normally use Microsoft compilers and have never encountered a vtable problem with them. If you use a compiler that has such a problem then get yourself a different compiler.
can I happily assume that the C++ language will handle that detail correctly without my intervention?
Yes. The implementation is responsible for setting the vtable pointer correctly during construction.
Just make sure that you do not treat C++ objects as 'raw bits'. For example:
struct A
{
virtual ~A() {}
A() { std::memset( this, 0, sizeof(A) ) ; /* NO! */ }
A( const A& that ) { std::memcpy( this, &that, sizeof(A) ) ; /* NO! */ }
A& operator= ( const A& that )
{ std::memcpy( this, &that, sizeof(A) ) ; /* NO! */ return *this ; }
void write( std::ostream& stm ) const
{ stm.write( reinterpret_cast<const char*>(this), sizeof(A) ) ; /* NO! */ }
void read( std::istream& stm )
{ stm.read( reinterpret_cast<char*>(this), sizeof(A) ) ; /* NO! */ }
int i ;
long l ;
char c[128] ;
};
You may want to have a look at Lippman's 'Inside the C++ Object Model' http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545
Ancient Dragon and vijayan121 thank-you for your answers and book recommendation. Inside the C++ Object Model I'll have to check out that book..Again thanks for your answers..G
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.