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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:46:54+00:00 2026-05-20T10:46:54+00:00

In Delphi we have an option to do a thing like this: TClass1 =

  • 0

In Delphi we have an option to do a thing like this:

TClass1 = class
  procedure Test; virtual;
end;

TClass2 = class(TClass1)
  procedure Test; override;
end;

So in code, If I create an instance of TClass2, even if I cast the object like:

TClass1(ObjectClass2).Test;

The application will call the function declared on TClass2.

But in C/C++ I could not find a way to do this.

If I declare some void as virtual and implement the same void in the children class when I do the cast to the parent class it’ll not use the implementation of the children class.

Does anyone know how I can reproduce the behavior of Delphi in C/C++ ?


New informations:
These are my files.

---------------------- File Filho.hpp
#ifndef FILHO_HPP
#define FILHO_HPP
#include "Pai.hpp"

class Filho : public Pai {
public:
    Filho();
    virtual ~Filho();
    void metodoX();
};

Filho::Filho() {}
Filho::~Filho() {}

void Filho::metodoX() {
    std::cout << "Hello Filho!" << std::endl;
}
#endif


---------------------- File Pai.hpp
#ifndef PAI_HPP
#define PAI_HPP
#include <iostream>

class Pai {
public:
    Pai();
    virtual ~Pai();
    virtual void metodoX();
};
Pai::Pai() {}
Pai::~Pai() {}

void Pai::metodoX() {
    std::cout << "Hello Pai!" << std::endl;
}

#endif

---------------------- File Main.hpp
#include "Pai.hpp"
#include "Filho.hpp"

int main() {
    Pai pai;
    pai.metodoX();      //Here output the msg Hello Pai!

    Filho filho;           
    filho.metodoX();    //Here output the msg Hello Filho!

    ((Pai) filho).metodoX(); //Here output the msg Hello Pai! , but here if I use the directive 'override' in Delphi, the output will be Hello Filho!. Here is my doubt.
    return 0;
}
  • 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-20T10:46:55+00:00Added an answer on May 20, 2026 at 10:46 am

    I’m not a Delphi expert, but I can explain how this stuff behaves in C++.

    So in C++, you can have a class that defines a virtual function, which means that if you use a base class pointer/reference to an object, that function can be invoked via dynamic dispatch (i.e. runtime function lookup).

    #include <iostream>
    
    class BaseClass
    {
    public:
        virtual void virtFunc() { std::cout << "BaseClass\n"; } // notice the 'virtual' keyword
        void nonvirtFunc() { std::cout << "BaseClass\n"; }
    };
    
    
    class SubClass : public BaseClass
    {
    public:
       virtual void virtFunc() { std::cout << "SubClass\n"; }
       void nonvirtFunc() { std::cout << "SubClass\n"; }
    };
    
    int main()
    {
        // You need to use base class pointers/references
        SubClass sc = SubClass();
        BaseClass *bcp = &sc;
        bcp->virtFunc(); // prints "SubClass"     
        bcp->nonvirtFunc(); // prints "BaseClass"   
    
        // doing it by allocating an object on heap
        BaseClass *dbcp = new SubClass();
        dbcp->virtFunc(); // prints "SubClass"
        dbcp->nonvirtFunc(); // prints "BaseClass"
        delete dbcp; // in a real program, you should have a virtual destructor which will be called from this code
    
        BaseClass bc = SubClass();
        bc.virtFunc(); // prints "BaseClass", and in more complex objects, slicing occurs
    }
    

    New Code You Posted

    I see that you updated with your code that does this:

    ((Pai)filho).metodoX();
    

    So when you do that, you are not using pointers/references to a base class. You are just casting the filho object to a Pai object. This does not result in a polymorphic function call, and instead will just call the Pai::metodoX() function.

    If you did this instead:

    ((Pai*)filho)->metodoX();
    

    It would call Filho‘s metodoX() polymorphically.

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

Sidebar

Related Questions

My code is littered with things like this Write (thePhpFile, ' echo <option value=\'
Is it possible in Delphi to have a class method invoke an inherited instance
I noticed that when I install Delphi(6,7,2000,2010), I have the option to install powerpoint
I have a Delphi Apache Shared Module. The base class is TWebModule. I can
I have a case in Delphi such as: Unit A contains class One Unit
We have some legacy code that compiles in Delphi 6. There are plans to
I'm writing an application in Delphi 2010, and I'd like to provide the option
I have a Delphi 7 application that has two views of a document (e.g.
I have an old Delphi codebase I have to maintain, lots of DLLs, some
I have a Delphi DLL with a function defined as: function SubmitJobStringList(joblist: tStringList; var

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.