0

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?

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by gerard4143
Featured Replies
  • 2

    [QUOTE]can I happily assume that the C++ language will handle that detail correctly without my intervention?[/QUOTE] 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: [CODE]struct A { virtual ~A() {} … Read More

0

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.

Edited by Ancient Dragon: n/a

2

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.