Serving the Quantitative Finance Community

 
User avatar
nrhiati
Topic Author
Posts: 0
Joined: July 14th, 2002, 3:00 am

how to call a pure virtual function

February 22nd, 2003, 5:45 pm

I was asked this questioon in an iinterview??Didn't know the answer.Any help?????
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

how to call a pure virtual function

February 22nd, 2003, 6:09 pm

Oh dear, perhaps you should have read the Careers topic before your interview...
 
User avatar
newton
Posts: 0
Joined: November 23rd, 2002, 5:46 pm

how to call a pure virtual function

February 22nd, 2003, 10:40 pm

I'm not sure the question makes much sense anyway. The issue is with 'pure', whichdirects the compiler to leave space for the virtual function pointer on the v-table and to create only aformal parameter list.So what does it mean to call something, the pure virtual function, that's a compile time directive??Did the interviewer also ask, "When did you stop beating your wife?"
Last edited by newton on February 21st, 2003, 11:00 pm, edited 1 time in total.
 
User avatar
jamesbattle
Posts: 0
Joined: May 12th, 2002, 8:28 pm

how to call a pure virtual function

February 24th, 2003, 8:55 am

Assuming that the pure virtual function f is declared in a class X, thena class derived from X will have to implement f (i.e. it's like an interface in languages such as C#).When an instance of any one of the classes derived from X is passed via a reference or pointer as an 'X' (* or &) it will be perfectly legal to call f in the usual way. At run-time, the correct implementation of this 'virtual function' will be used and this was probably the point of the question...The linker will usually prevent invalid uses of virtual functions at compile-time,though some can only be caught at run-time (usually by an abort) if the libraries get out of synch...
 
User avatar
DominicConnor
Posts: 41
Joined: July 14th, 2002, 3:00 am

how to call a pure virtual function

February 24th, 2003, 9:08 am

Indeed, the linker catches a lot of this. However, Microsoft COM uses vtables, and these can become really quite confused dynamically.
 
User avatar
newton
Posts: 0
Joined: November 23rd, 2002, 5:46 pm

how to call a pure virtual function

February 24th, 2003, 11:40 am

The point is that virtual functions can be called, pure virtual functions cannot; they just forcean implementation a derived class. If fact a base class with a pure virtual will not even compile.Remember you call the implemenations not he definition!!!!You cannot call an interface (a definition), it just defines the functions that must be implemented.
Last edited by newton on February 23rd, 2003, 11:00 pm, edited 1 time in total.
 
User avatar
jamesbattle
Posts: 0
Joined: May 12th, 2002, 8:28 pm

how to call a pure virtual function

February 26th, 2003, 12:48 pm

There is NOTHING that prevents 'calling' a pure virtual function. Consider the following (legal) class definition:class A{public: virtual void f() = 0; void g() { f(); }};This will compile without error and displays the pattern for a common usage of pure virtual functions. i.e. that in an abstract base, useful work might be able to be accomplished through calling a pure virtual. Of course, at RUN-TIME, the callto f() will be implemented by a class derived from A.Ditto for interfaces, e.g. in C# rather than C++. It is perfectly legal to pass anargument through an interface and at run-time the actual call will resolve to theclass that -implements- the interface.
 
User avatar
Simplicio

how to call a pure virtual function

February 26th, 2003, 1:51 pm

I think the point being made is that one can actually have a situation where a function is called which has no implementation at that point. So yes, pure virtual is a compile time concept in that it makes sense for abstract classes only, and abstract classes are not instantiable, but it also has a run time meaning in that classes are constructed via their base classes, and at some points the embryo class doesn't yet have the implementation of the virtual function to hand. Normally a virtual function call resolves to a function implementation, but if one is careless enough to call pure virtual functions in constructors then one can get a situation where the embryo is still a base class and tries to call a function which is implemented in the concrete class. Therefore the following really does call a pure virtual function, i.e. a virtual function which has no resolvable definition at the point of calling:class Abstract{ public: Abstract() {initialise();} virtual void initialise() = 0;};class Concrete : public Abstract{ public: virtual void initialise() {}};somescope{ Concrete concrete; // oops, pure virtual function called}
 
User avatar
newton
Posts: 0
Joined: November 23rd, 2002, 5:46 pm

how to call a pure virtual function

February 26th, 2003, 6:03 pm

jamesbattle,You should remember calling only happens at run time.Obviously this class, Abstract, can't be instantiated; next you'll be saying that you call definitions in .h files.Only functions in concrete classes may be called.-newton
 
User avatar
Simplicio

how to call a pure virtual function

February 27th, 2003, 11:26 am

Newton << I didn't say that Abstract was instantiated; being instantiated and having your constructor called are not the same thing.However I guiltily confess that I wrote that stuff based on memory - it used to work; after writing I thought I would check it and it doesn't work in VC6.0 and probably other modern compilers. Nevertheless the same effect can be obtained by passing around this within construction code:class Abstract{public:Abstract() {initialise(*this);}void initialise(Abstract &abstract){abstract.naughty();}virtual void naughty() = 0;};class Concrete : public Abstract{public:virtual void naughty() {}};somescope{Concrete concrete; // oops, pure virtual function called}This will give the following output:runtime error R6025- pure virtual function callOf course why one would do this boggles the mind.
 
User avatar
newton
Posts: 0
Joined: November 23rd, 2002, 5:46 pm

how to call a pure virtual function

February 27th, 2003, 12:15 pm

Simplicio,My only point was that 'pure' is used for function definition as in a .h files, and only virtual functions areactually called. I surely was not complaining about your example, perhaps far fetched, but it does againshow that only virtual functions are called.Thanks
 
User avatar
jamesbattle
Posts: 0
Joined: May 12th, 2002, 8:28 pm

how to call a pure virtual function

February 27th, 2003, 12:42 pm

Without wanting to debate this, where exactly in the C++ standard did you get the definition of 'call'? I have never seen such a definition, except possibly in FORTRAN where it is a keyword for 'invoking' a sub-routine. Is this definition of 'call' in fact'your' definition?There is nothing special about a pure virtual function. In fact it is identical to theeffect of the 'abstract' keyword on a virtual function in languages such as C#.Arguably this would have been a better way to present the syntax in C++, ratherthan the highly unusual '= 0' syntax after a virtual function.Secondly, there is nothing in the C++ language standard which differentiates a'header' (a.k.a .h) file from a source (a.k.a .cpp) file.Finally, I think it is unfortunate that these C++ threads so often degenerate into insult.
 
User avatar
newton
Posts: 0
Joined: November 23rd, 2002, 5:46 pm

how to call a pure virtual function

February 27th, 2003, 1:03 pm

Term 'call' came from the person who was ostensibly interviewing nrhiati.