C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-05-2003, 10:50 AM   #1
Registered User
 
Join Date: Feb 2002
Posts: 591
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.
Barjor is offline   Reply With Quote
Old 06-05-2003, 11:51 AM   #2
the hat of redundancy hat
 
nvoigt's Avatar
 
Join Date: Aug 2001
Location: Hannover, Germany
Posts: 2,769
Well, you could make Intersect an abstract function as well like this:

public abstract Vector Intersect( Entity other );

And overwrite this for each derived class, so you can do:

m_Entity1->Intersect( m_Entity2 );
__________________
hth
-nv

She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

When in doubt, read the FAQ.
Then ask a smart question.
nvoigt is offline   Reply With Quote
Old 06-05-2003, 01:01 PM   #3
Registered User
 
Join Date: Feb 2002
Posts: 591
What I am trying to do is to create overloaded functions that will take care of intersections between Arcs and line

public Vector Intersect( Line, Line );
public Vector Intersect( Line, Arc );
public Vector Intersect( Arc, Line );
public Vector Intersect( Arc, Arc );

when I call them I would like to just be able to pass them two Entity and have the run time decide what function to call. The problem is that without some sort of type checking I don't know what type (arc or line) that will be passed to the Intersect(XX,XX)


"Quote"
m_Entity1->Intersect( m_Entity2 );

Done to much C++ lately??? *Smile*

Thanks for your help



EDIT

MM maybee I was reading that to fast..you might actually have solved my problem...I think...maybee

THANKS

~Barjor

Last edited by Barjor; 06-05-2003 at 01:04 PM.
Barjor is offline   Reply With Quote
Old 06-05-2003, 11:53 PM   #4
the hat of redundancy hat
 
nvoigt's Avatar
 
Join Date: Aug 2001
Location: Hannover, Germany
Posts: 2,769
Oops *ggg* yeah, I had to find my way through another projects C++ code all day...
__________________
hth
-nv

She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

When in doubt, read the FAQ.
Then ask a smart question.
nvoigt is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting an error with OpenGL: collect2: ld returned 1 exit status Lorgon Jortle C++ Programming 6 05-08-2009 08:18 PM
Problem in compiling targa2.c: error: field `ip' has incomplete type.. Moony C Programming 0 03-20-2008 07:59 AM
Question on l-values. Hulag C++ Programming 6 10-13-2005 04:33 PM
Erros in Utility Header File silk.odyssey C++ Programming 4 12-22-2003 06:17 AM
Warnings, warnings, warnings? spentdome C Programming 25 05-27-2002 06:49 PM


All times are GMT -6. The time now is 10:30 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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