Thread: Conversion constructor for template class

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This holds for all 'Real' objects when derived from 'Complex' : conjugate( x), imag( x), real( x) and so forth.
    Well, it does depend on what behaviour you provide. For example, Complex might have a member function to get the imaginary portion. That does not make sense for a Real. On the other hand, you could say that Real objects do have an imaginary portion that is 0... but then what's the point of a Real class when Complex will do?

    I think the decision on whether to keep everything to Complex or have both Real and Complex depends on your set of vector spaces thing that you are trying to model. If you can model that with just Complex, then it would be simpler (heh, what irony) to just use Complex.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This holds for all 'Real' objects when derived from 'Complex'
    Absolutely not. One property of a Complex object would probably be: "It's possible to set the imaginary part to an arbitrary value."
    A Real does not have this property, because if the value is anything but 0, it wouldn't be a Real anymore.

    Neither does it work the other way round. One very important property of Reals is: "There exists a total order '<' for the entire domain of real numbers."
    But less-than is not defined for complex numbers.

    It's true that just because the LSP is satisfied it doesn't mean that class derivation is automatically the correct solution. But what does hold is that if the LSP is not satisfied, derivation is definitely not the correct solution.
    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

  3. #18
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Shame on me! You're right, I forgot about the ordering '<'.

    Mathematically, a subset/superset relation designates an "is a" relation. This "is a" relation is exactly what designates a derivation, as "they" have always told me.
    At the same time, a complex number "has" two real numbers, as the functions 'imag( z)' or 'real( z)' should return a real number. I think this simultaneous "is a" and "has a" relation is confusing if not wrong.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    "They" are wrong. A derivation requires "is-a", but "is-a" is not sufficient for derivation. The "is-a" of derivation is one of abstraction: a dog "is-a" animal. That's not because dogs are a subset of animals, but because animal is a more abstract concept than dog.
    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

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    "Works-like-a" is often used in addition to or instead of "is-a" when explaining when to use public derivation. "Works-like-a" obviously more closely matches the crux of the LSP.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer conversion problems with a copy constructor
    By stanlvw in forum C++ Programming
    Replies: 8
    Last Post: 01-14-2008, 12:06 AM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  4. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM