First Class Objects, Polymorphic Objects, and Singletons

Here is a good explanation of First Class Objects, Polymorphic Objects, and Singletons that my FSU Professor, Dr. Chris Lacher posted.

Question received by other means:

In the requirements [for Project 1] you call for the copy constructor and the assignment operator to be private for each class. Shouldn’t they be public for the classes to be fully qualified types?

Yes. But there’s a lot more underneath this question. First some terminology:

  1. A class that is what we call “proper type” is often called a first class type.
  2. A type that is intended to be used in a polymorphic environment (such as our tracker project) is called a polymorphic type.
  3. A type for which there should be only one instance in a given namespace is called a singleton.

(These terms apply, ambiguosly, to either classes or objects.) All of these are related to the question - why make the copy constructor and assignment operator private, and what are the implications?

Singletons. Note that if the copy ctor and operator= are private, then no client program may use them. The effect is that a client program is not allowed to make copies of these objects - either explicitly (using operator=) or implicitly (using copy ctor). The client program attempting to call a function by value with one of these objects as argument, or assign one to another, will get a compile error. You prevent the class implementation code from making copies by not implementing these methods, so they too will get an error if one is used, albeit from the linker instead of the compiler. Thus both (1) making the copy ctor and operator= private and (2) omitting implementations for them will serve to make the object a singleton. (There is more to it than this, but this is an excellent start.)

Polymorphic objects. In polymorphism, if object copies are allowed, they are usually made with a public member function named Clone() that returns a pointer to a copy of this object. In this situation, you want the copy ctor and operator= private, but you do provide implementations. Then you have a method Clone() that is implemented something like:

X* X::Clone {return Xptr = new X (*this);} // calls copy ctor

Note that in a polymorphic setting, we are typically using pointers to type X (and its derived classes). If the client program has two pointers, p1 and p2, and wants p2 to be a (deep) copy of p1, the code would be

p2 = p1->Clone();

First Class Objects. These are objects from a proper type. They behave just like a native type such as char, int, or float. Yu can assign to or from them and let them go out of scope with no unfortunate consequences.

What we have done in project 1 is make the various classes singletons. We could make the types polymorphic, but there was no need, and it’s a can of worms. You will run into these ideas again, either in school or professionally.

No Comment

No comments yet

Leave a reply

You must be logged in to post a comment.