Thread: Solving type of class

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    589

    Solving type of class

    This is kinda lengthy but I am curious if there is a neat solution to this

    I have a class like so
    Code:
    public abstract class Entity
    {
        public abstract void Draw(Graphics g);
        public static Vector Intersect(Line line1, Line line2)
        {
             //Do stuff
        }
        public static Vector Intersect(Arc arc1, Arc arc2)
        {
             //Do stuff
        }
    }
    Then I have two classes like this
    Code:
    public class Line : Entity
    {
        override public void Draw(Graphics g)
        {
            //Draw stuff
        }
    }
    
    public class Arc : Entity
    {
        override public void Draw(Graphics g)
        {
            //Draw stuff
        }
    }
    I now can do this
    Code:
    private void MainWin_Paint(e)
    {
        foreach(Entity entity in m_alVisableEntities)
        {
            Graphics g = e.Graphics;
            entity.Draw(g);	
        }
    }
    What happens here is that at run time the program call the correct draw function , either Arc or Line.

    But if I try to do this
    Code:
    vIntersection = Entity.Intersection(m_Entity1, m_Entity2);
    where m_Entity1 and m_Entity2 are Entity containg Line instances. I get a compile time error saying something like "cannot convert argument from Entity to Line"

    Why is it that in the first case the compiler can solve what type of entity I am calling but not in the second case. And is there a decent way to solve it without manually doing type checking?

    Thanks

    ~Barjor
    Last edited by Barjor; 06-05-2003 at 11:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM