Method and system for implementing virtual functions of an...

Data processing: software development – installation – and managem – Software program development tool – Programming language

Reexamination Certificate

Rate now

  [ 0.00 ] – not rated yet Voters 0   Comments 0

Details

C717S136000, C709S241000, C709S241000

Reexamination Certificate

active

06704924

ABSTRACT:

BACKGROUND
Object-oriented programming techniques allow for the defining of “interfaces” by which object can expose their functionality independently of the implementation of the functionality. In the C++ programming language, an interface is defined by an abstract class whose virtual functions are all pure. A pure virtual function is one that has no implementation in the class. Thus, an interface defines only the order of the virtual functions within the class and the signatures of the virtual functions, but not their implementations. The following is an example of an interface:
class IShape
{
virtual void draw(int x,y)=0;
virtual void save(char filename)=0;
virtual void clear(int x,y)=0;
}
This interface, named “IShape,” has three virtual functions: draw, save, and clear. The “=0” after the formal parameter list indicates that each virtual function is pure. Concepts of the C++ programming language that support object-oriented programming are described in “The Annotated C++ Reference Manual,” by Ellis and Stroustrup, published by Addison-Wesley Publishing Company in 1990, which is hereby incorporated by reference.
Once an interface is defined, programmers can write programs to access the functionality independent of the implementation. Thus, an implementation can be changed or replaced without having to modify the programs that use the interface. For example, the save function of the IShape interface may have an implementation that saves the shape information to a file on a local file system. Another implementation may save the shape information to a filer server accessible via the Internet.
To ensure that an implementation provides the proper order and signatures of the functions of an interface, the class that implements the interfaces inherits the interface. The following is an example of a class that implements the IShape interface.
class Shape : IShape
{
virtual void save(char filename){. . .};
virtual void clear(int x,y){. . .};
virtual void draw(int x,y){. . .};
virtual void internal_save(){. . .};
int x;
int y;
}
The first line of the class definition indicates by the “: IShape” that the Shape class inherits the IShape interface. The ellipses between the braces indicate source code that implements the virtual functions. The Shape class, in addition to providing an implementation of the three virtual functions of the IShape interface, also defines a new virtual function “internal_save,” which may be involved by one of the implementations of the other virtual functions. The Shape class also has defined two integer data members, x and y.
Typical C++ compilers generate virtual function tables to support the invocation of virtual functions. When an object for a class is instantiated, such a C++ compiler generates a data structure that contains the data members of the object and that contains a pointer to a virtual function table. The virtual function table contains the address of each virtual function defined for the class.
FIG. 1
illustrates a sample object layout for an object of the Shape class. The object data structure
101
contains a pointer to a virtual function table and the data members x and y. The virtual function table
102
contains an entry for each virtual function. Each entry contains the address of the corresponding virtual function. For example, the first entry in the virtual function table contains the address of the draw function
103
. The order of the references in the virtual function table is the same as defined in the inherited interface even though the Shape class specifies these three functions in a different order. In particular, the reference to the draw function is first, followed by the references to the save and clear functions.
The C++ compiler generates code to invoke virtual functions indirectly through the virtual function table. The following is C++ code for invoking a virtual function.:
Shape
*
pShape
=
new



Shape
;
pShape

save

(

)
;
The pointer pShape points to an object in memory that is of the Shape class. When compiling this invocation of the virtual function, the compiler generates code to retrieve the address of the virtual function table from the object, to add to the retrieved address the offset of the entry in the virtual function table that contains the address of the function, and to then invoke the function at that calculated address.
The inheritance of interfaces allows for references to objects that implement the interfaces to be passed in an implementation independent matter. A routine that uses an implementation may define a formal argument that is a pointer to the IShape interface. The developer of the routine can be unaware that the implementation is actually the Shape class. To pass a reference to an object of the Shape class, a program that invokes the routine would type cast a pointer to the object of the Shape class to a pointer to the IShape interface. So long as the pointer points to a location that contains the address of the virtual function table and the virtual function table contains the entries in the specified order, the invoked routine can correctly access the virtual functions defined by the IShape interface:
Although the use of interfaces facilitates the development of programs that use and implement interfaces, difficulties do arise. The following C++ source code illustrates one of these difficulties.
class A
{
virtual void A1()=0;
}
class B : A
{
virtual void B1()=0;
}
class AImp : A
{
virtual void A1(){. . .};
virtual void A2(){. . .};
}
class BImp : AImp, B
{
virtual void B1(){. . .};
}
In this example, classes A and B are interfaces. Interface A defines virtual function A
1
, and interface B defines virtual function B
1
. Class AImp is an implementation of interface A and defines a new virtual function A
2
. Class BImp is an implementation of interface B that inherits the implementation of interface A, which is class AImp. Thus, class BImp inherits multiple classes: class AImp and interface B.
Some C++ compilers have the option of merging the virtual function tables of multiple base classes into a single virtual function table in the derived class or of generating separate virtual function tables for each base class. One difficulty arises if the virtual function tables of the base classes are merged in the derived class.
FIG. 2
illustrates the difficulty when the virtual function tables are merged. The object layouts
201
and
202
illustrate the order of the virtual functions within the virtual functions tables. (Since interfaces A and B are abstract classes, objects of these classes cannot actually be instantiated.) To allow type casting as described above, an implementation of interface B needs to provide a virtual function table with entries in the same order as shown in object layout
202
, that is with the entry for virtual function A
1
followed immediately by the entry for virtual function B
1
. A problem arises when the implementation of interface A, which is class AImp, defines a new virtual function, such as virtual function A
2
. The virtual function table of class AImp is shown by object layout
203
. Object layout
204
illustrates the virtual function table for class BImp when the virtual function tables are merged. This virtual function table for class BImp illustrates the order of the entries to be virtual functions A
1
, A
2
, and B
1
. If a pointer to an object of class AImp is type cast to a pointer to interface B, then the virtual function table would not have the entries in the order as specified by the interface B. In particular, the entry for virtual function A
1
is not immediately followed by the entry for virtual function B
1
. A C++ compiler could have reordered in entries in the virtual function table within object layout
204
to be virtual functions A

LandOfFree

Say what you really think

Search LandOfFree.com for the USA inventors and patents. Rate them and share your experience with other people.

Rating

Method and system for implementing virtual functions of an... does not yet have a rating. At this time, there are no reviews or comments for this patent.

If you have personal experience with Method and system for implementing virtual functions of an..., we encourage you to share that experience with our LandOfFree.com community. Your opinion is very important and Method and system for implementing virtual functions of an... will most certainly appreciate the feedback.

Rate now

     

Profile ID: LFUS-PAI-O-3266546

  Search
All data on this website is collected from public sources. Our data reflects the most accurate information available at the time of publication.