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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:21:27+00:00 2026-05-25T13:21:27+00:00

I’m working with Lua and C++ (using LuaWrapper) and I’m having trouble with something.

  • 0

I’m working with Lua and C++ (using LuaWrapper) and I’m having trouble with something. Let’s say I have these classes:

class Bar{...};
class Foo
{
    ...
    std::map<std::string,Bar*> _barlist;
    ...
    Bar* getBar(std::string key)
    {
        return _barlist[key];
    }
}

class BarDerived1 : public Bar{...}
class BarDerived2 : public Bar{...}

I have access to Foo and Foo::getBar() from Lua , the thing is that the type from the object returned is Bar and I don’t have access to its BarDerivedX methods.

As I understand it, Objects/Classes in Lua are simply MetaTables with functions assigned to keys, so I was wondering if there’s a way to copy these extra functions from the BarDerivedX class to the Bar object (and would that even work?), also, is this the best way to do it or should I cast it in C++ before letting Lua access it?

  • 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-25T13:21:28+00:00Added an answer on May 25, 2026 at 1:21 pm

    The purpose of polymorphism, in any language, is to not have to care exactly what type something is. If you give someone, whether Lua or C++, a Bar object, then Bar should have the interfaces that the people need to do whatever they need to do.

    That’s the purpose of virtual functions. A virtual function defined in Bar can be overridden with a different behavior in BarDerived1. Other code doesn’t have to know that they are being handed a derived class; they can just take a Bar like always.

    There are generally only two reasons to make a class derived from another. The first is polymorphism: you want to specialize a class by altering the behavior of certain functions.

    The other reason is to gain access to its implementation. For example, if you’re making a Unicode string, you could use a std::basic_string to store the encoded data. You could then privately derive from std::basic_string, thus allowing you to gain the storage optimizations that std::basic_string has, but you provide a different interface (since private inheritance doesn’t allow the outside world to know that you derived from it).

    If you want everyone to use Bar objects that can have different behaviors based on derived classes, then you should be using virtual functions and polymorphism. However, if you are changing the interface in derived classes, then that’s probably a sign of a design problem. Maybe those derived classes shouldn’t be derived classes at all. Maybe they should be entirely separate classes that a Bar instance may or may not have, based on some initialization parameters.

    It’s hard to say more without specifics on what you’re actually doing. But in general, if you’re using inheritance-based polymorphism correctly, you shouldn’t need to cast from a Bar to more derived versions. Just use the Bar interface.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,

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.