Thread: rewrite const property through overloading

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    CornedBee: the underpinning of type theory is actually category theory, which is a branch of discrete mathematics. There is very little in mathematics that can't be found to have practical value (although "practical" does not mean the same thing as "understandable to everyone") ....

    George: Let's say we have a Base class with a virtual method that returns a pointer to Base, and two Derived classes Derived1 and Derived2 that override that method. So the return value from Base::method() is a Base*, return value from Derived1::method() is a Derived1*, and Derived2::method() returns a Derived2.

    If X is the set of classes [Base, Derived1, Derived2] with some ordering so it is considered that Base < Derived1 < Derived2. A simple encoding would be to represent them as distinct integers. Let's say [1,2,3].

    Similarly Y denotes the set of return types from the method, namely [Base*, Derived1*, Derived2*] which may be encoded as a set of integers too. Let's say [4,5,6]. This is actually a covariant example (Base->Base*, Derived1->Derived1*, Derived2->Derived2*). The set of pairs (X,Y) are [(1,4), (2,5), (3,6)]. Since the mean of X is 2 and mean of Y is 5, the covariance may be computed as Cov(X,Y) = ((-1)*(-1) + 0*0 + 1*1)/3 = 2/3 (a positive value).

    One type of contracovariance (which, as I think CornedBee said earlier, is not possible in C++) would be to reverse the relationship between return value and the containing class. For example, if the return value of Base::method() is Derived2*, Derived1::method returns Derived1, and Derived2::method() returns Base. This would map to a set of pairs (X,Y) which is [(1,6), (2,5), (3,4)] from which Cov(X,Y) = -2/3.

    The key in the representation is picking a distinct ordering relationship for types, and ensuring a unique value for each distinct type. There is also a reliance on invariance relationships. For example, in the above, the relationship Base < Derived1 < Derived2 invariably implies the relationship Base* < Derived1* < Derived2*
    Last edited by grumpy; 02-26-2008 at 02:35 AM.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by George2 View Post
    1. Beyond the pattern of decend/ascend in return type/class hierarchy as we pointed out, is it the only pattern of covariance? Are there any else patterns in C++ which could be called covariance?
    Nothing else in C++ is called covariance.

    2. Interested in Contravariance sample, does it have practical usage scenario?
    Since it isn't supported in any language I know, I've never given it thought.

    primary varying item is the class hierarchy base/derived class, and the secondary (varying item) is the return type (in derived class, using derived class type pointer, but in base class, using base class type pointer?)?
    Yes. Note that the hierarchy that varies in the return type doesn't have to be the same as the hierarchy that contains the method, as shown in by foo/bar sample.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM