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 3304754
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:05:44+00:00 2026-05-17T21:05:44+00:00

I’m trying to make a class inherits from other and override some methods. Classes

  • 0

I’m trying to make a class inherits from other and override some methods. Classes ‘header’ is:

class Objeto {  
public:  
    virtual bool interseca(const Rayo &rayo, float magnitud);  
    virtual bool breakNormal(const Punto &punto);  
    virtual Vector normal(const Punto &punto);  

    int idMaterial;  
};

class Esfera: public Objeto {
public:
    int idMaterial;

    virtual bool interseca(const Rayo &rayo, float magnitud);
    // etc
};

Next in other place of the program (outside of Objeto and Esfera) I do:

// ObjectList is a Vector<Objeto>
Objeto o = esfera; /* Where esfera is a valid Esfera object */
ObjectList[0] = o;
ObjectList[0].interseca(rayo, magnitud);

What I want is to call the new version of interseca that is in Esfera. In this way I can add more objects (Cube, Triangle, etc) and treat them as generic “Objetos”.

But instead of the Esfera implementation of interseca the program calls Objeto::interseca.

What is the correct way to do this override with C++? Is that the way to do overriding and I’m missing something or I’m plain wrong? Any tip or alternative to do that?

  • 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-17T21:05:44+00:00Added an answer on May 17, 2026 at 9:05 pm

    You can only get polymorphic behavior if you access the class via a pointer or reference. In addition to that, you are slicing the object when you are copying the derived type to a non-pointer/non-reference base type.

    Slicing:

    http://en.wikipedia.org/wiki/Object_slicing

    Given these definitions:

    #include<iostream>
    
    class Objeto {  
    public:  
        virtual bool interseca() {
            return false;
        }
    };
    
    class Esfera: public Objeto {
    public:
        int idMaterial;
    
        virtual bool interseca() {
            return true;
        }
    };
    

    This won’t do what you want:

    Esfera e;
    Objeto o = e;
    std::cout << o.interseca() << "\n";
    

    false

    But this will:

    Esfera e;
    Objeto& o = e;
    std::cout << o.interseca() << "\n";
    

    true

    Program design

    A technique you can use to avoid this in the future is to make your base classes (pure) abstract.

    Would you ever instantiate a true Objeto in your scene, or are you simply defining a base type? If you are just defining a base type (which I recommend), then you can make interseca, breakNormal, and normal pure virtual. Then, the compiler will catch problems like the one you have here.

    class Objeto {  
    public:  
        virtual bool interseca() = 0;
    };
    
    class Esfera: public Objeto {
    public:
        int idMaterial;
    
        virtual bool interseca()
        {
            return true;
        }
    };
    
    // ...
    

    Then, this will be okay:

    Esfera e;
    Objeto& o = e;
    std::cout << o.interseca() << "\n";
    

    compilation succeded

    But this will cause the compiler to error – a good thing, cause it’s catching a bug:

    Esfera e;
    Objeto o = e;
    std::cout << o.interseca() << "\n";
    

    error: cannot allocate an object of abstract type ‘Objeto’

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

Sidebar

Related Questions

No related questions found

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.