TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Bob
NA
1
0
Writing a raytracer to learn C#.... hit a problem with inheritance / iteration:
May 8 2008 5:03 PM
I am writing a simple raytracer in C# as a way of learning the language - but have run into a problem:
I have a parent class called "Primitive" which has an "Intersection" function.
I have multiple child classes, for example "Sphere" which overwrite the "Intersection" function with "new public Intersection... etc..." as their intersection methods are all quite different.
This works fine for single instances - but when I create an arraylist of primitives, such as Spheres and Planes, and the iterate through them with a foreach like this:
ArrayList PrimList = new ArrayList();
Sphere sphere = new Sphere ();
PrimList.Add (sphere);
Plane plane = new Plane();
PrimList.Add (plane);
foreach (Primitive p in PrimList)
{
i =p.Intersect (x);
}
This will always use the parent intersect function and not the appropriate child function. :/
I think it's because the Spheres are being recasted to Primitives by the foreach function....
Is there a way around this such that the correct intersection method is used?
Reply
Answers (
1
)
Debugging windows service running on remote machine
Best way to come grasp with C# when coming from java world