Conversion constructor for template class

This is a discussion on Conversion constructor for template class within the C++ Programming forums, part of the General Programming Boards category; This holds for all 'Real' objects when derived from 'Complex' : conjugate( x), imag( x), real( x) and so forth. ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,634
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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,893
    "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,317
    "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.

Page 2 of 2 FirstFirst 12
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-13-2008, 11:06 PM
  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
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21