开发者

Can an Object-oriented type system be implemented by a language with an Object-oriented type system?

Suppose you have an imaginary type system for an imaginary scripting language which is written in C++ (for example), and each type (and object) in the scripting language has a corresponding type (and object) in the underlying implementation language. The base class in this imaginary type system is a class called Object, and all other classes must derive from this class. Now, you have another class called HashTable, which is the basis for all variable storage (I might have said that wrong): namespaces are implemented via HashTables (associating one object with another object), global variables are stored via HashTables, and, to the point of the problem, instance variables are also stored in HashTables.

Ins开发者_如何学编程tance variables are such that every Object has an HashTable in which to store its instance variables. However, HashTable necessarily derives from Object, therefore each HashTable has an HashTable in which to store its instance variables. And every HashTable for every HashTable has an HashTable, and so on ad infinitum.

My question is, can this type system be implemented in an object-oriented way in the underlying C++ code? If no precautions are taken, the program will enter an infinite loop and cause a stack overflow on the mere instantiation of an Object, because it will instantiate an HashTable which will call its parent constructor for Object, which will instantiate an HashTable...

Are there any viable workarounds for this design flaw which don't involve breaking the desired OO design (each type has its corollary type in the underlying code)?

Pardon the grammar in this post, English is not my first language and I might not have explained something in a comprehensible manner.


implement two different HashTable types: one for user code (UserHashTable), derived from Object and so no violating your "everything is Object" rule, and another for internal use (CoreHashTable) to implement your type system.

[EDIT] CoreHashTable can be automatically convertible to UserHashTable, e.g. UserHashTable can contain internal smart pointer to CoreHashTable.


Yes. You can emulate your own "object system" with another programing language.

That concept its called a "virtual object system".

O.O. Programming languages have its own "Object System". By an "Object System", I don't mean "O.O. libraries or O.O. Class hierarchy". By an "Object System" I mean its way of declaring and using classes and objects.

But, sometimes, your programming language its not object oriented, or even if its object oriented, some stuff is missing. C# and Java doesn't have real properties & events, C# and Object Pascal does.

When O.O. started, many programmers use non O.O. programming languages, and learn about O.O. Some make their own "plain C" to "C++" preprocessors (Objective-C maybe), some did full compilers.

And some emulate them. The programmer, conceptually, thinking, he was using classes and objects, but, in code, use structures & pointers.

I have seen several "virtual object system (s)", where a group of classes or objects is simulated by another programming language.

Once, I worked with a database conectivity tool called "Borland Database Engine", where developers read and write data into objects like "databases", "tables", "fields".

One famous, is the GLib library ("GObject" is the root object), used in the GNome visual interface for GNU/Linux. It's done in "plain C", but simulates objects and classes, using pointers.

More Info: http://en.wikipedia.org/wiki/Gobject

You want to use a group of objects conceptually speaking, but in code, you won't have a class declaration for your conceptual class, but, some data stored in hash tables, using another O.O. programming language. Yes it can be done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜