Letters

Letters

By Mike Toms, Adrian Fagg

Overload, 2(5):21, September 1994


Dear Mike,

Congratulations on the first four issues of Overload, I would like to add a couple of comments to your answers in the last two issues (in a spirit of .pedantry only). In Issue 3 P34 Q4 you appear to have overcomplicated the solution with conditional compilation!

Simply declare the const array as extern, declare the class using the const array name as the default argument and define the const array in the source file as for any initialised data:

hello.hpp:

extern const char name[];
void print( const char str[] = name );

hello.cpp:

#include "extconst.hpp"
#include <iostream.h)
const char name[] = {"Hello World"};
void print( const char str[] )
{
 cout << str;
}

void main()
{
 print();
}

In issue 4 p44 Q10 you say “… if polymorphic member function is called, only the base member function will be called”. I’m sure it was just a slip of the tongue but in fact it will be the constructors own version of the function that is called, whether it happens to be the base class or not. Interestingly, although the virtual calling mechanism is supposed to be turned off, the virtual method tables are still used to call virtual functions.

The way it works is that as each base object is constructed, the virtual method pointer is set to the table for that class, immediately prior to the body of the constructor being executed. This means that the method table will contain the addresses of the functions that would apply to an instance of the class of the constructor in use. This means that it isn’t possible to subvert this behaviour - even calling the functions via a base pointer in another function will only get the same method table. Interestingly it’s possible to call a pure virtual function this way without the compiler ever realising it - you’ll find out at run-time of course!

Adrian Fagg

Thank’s for the corrections and information.

Mike Toms.






Your Privacy

By clicking "Accept Non-Essential Cookies" you agree ACCU can store non-essential cookies on your device and disclose information in accordance with our Privacy Policy and Cookie Policy.

Current Setting: Non-Essential Cookies REJECTED


By clicking "Include Third Party Content" you agree ACCU can forward your IP address to third-party sites (such as YouTube) to enhance the information presented on this site, and that third-party sites may store cookies on your device.

Current Setting: Third Party Content EXCLUDED



Settings can be changed at any time from the Cookie Policy page.