Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6008403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:49:09+00:00 2026-05-23T01:49:09+00:00

I want to use inheritance to have an object treated in a different way

  • 0

I want to use inheritance to have an object treated in a different way depending on where in the hierarchy it falls

(similar to this C# question)

Assume you build a hierarchy of Shape objects like:

class Shape {} ;
class Sphere : public Shape {} ;
class Triangle : public Shape {} ; ...

You then equip a Ray class with methods like:

class Ray
{
    Intersection intersects( const Sphere * s ) ;
    Intersection intersects( const Triangle * t ) ;
};

You store an array of assorted Shape* of various types and call

vector<Shape*> shapes ; ...
//foreach shape..
Intersection int = ray.intersects( shapes[ i ] )

But you get the compiler error

error C2664: ‘Intersection Ray::intersects(const Sphere *) const’ : cannot convert parameter 1 from ‘Shape *const ‘ to ‘const Sphere *’

What did you do wrong?

Is the only way to do it the other way around, with

class Shape
{
    virtual Intersection intersects( const Ray* ray )=0 ;
} ;

then each class override intersects? Then calls to

//foreach shape..
Intersection int = shapes[i]->intersects( ray ) ;

Can you do it the first way I showed or NEVER?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:49:09+00:00Added an answer on May 23, 2026 at 1:49 am

    You have to do it the other way around. Overload resolution takes place at compile-time, when the type of what you’re calling it with is a Shape*.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With Yii php framework, I use inheritance. In my AbstractModel, I have this method:
I have a question similar to this but in the context of L2S. I
I want to use partials in rails along with single-table inheritance. I currently have
I have some object classes that use inheritance. It seems that I can only
I have been working on a multiple inheritance code which looks similar to this:
I have a question about inheritance. From this source: gSpan.h struct Edge { int
Could someone please explain when would I want to use delegation instead of inheritance?
I want use this 1 for using Bar code or QR code scanner. I
i want use some data from a website with web service. i have a
Hey guys. I have the following situation. I want to use a TypeDescriptor to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.