Note that there are some explanatory texts on larger screens.

plurals
  1. POType aliases in C++
    primarykey
    data
    text
    <p>I have a class <code>Point</code> and a class <code>Rect</code>. I want to have two constructors for the <code>Rect</code>: One with a <code>Point</code> and a dimension (width, height) and one with two Points (top left, bottom right). Now it turns out that <code>Point</code> also can be seen as a dimension, so instead of creating a <code>Dimension</code> class I want to use my <code>Point</code>, basically like this:</p> <pre><code>class Point{...}; typedef Dimension Point; class Rect{ public: Rect(Point, Point); Rect(Point, Dimension); } </code></pre> <p>So the question is: Does the compiler make a difference between <code>Point</code> and <code>Dimension</code>? I tried it, the message ist "call of overloaded Rect(Point, Point) is ambiguous.". </p> <p>How should I do that? Preferably without inheritance :)</p> <p><strong>EDIT</strong></p> <p>I understood now that it's the same to the compiler. But there is another scenario wher I need that.</p> <p>I have a Point. The coordinates can be in a carthesian system (x, y) or GPS coordinates (lon, lat). It's perfectly ok for me to call the components <code>x0</code> and <code>x1</code> so I want to use only one class. </p> <p>Now I want to calculate the distance between the two points and my idea is as follows:</p> <pre><code>typedef PointLonLat Point; typedef PointXY Point; double distance(PointLonLat, PointLonLat); double distance(PointXY, PointXY); PointLonLat p1(10, 10); PointLonLat p2(11, 11); double dist = distance(p1, p2); // the correct implementation is used </code></pre> <p>I know it doesn't work like that. But would the answer to that also be "make two classes"?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload