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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:08:43+00:00 2026-05-21T18:08:43+00:00

Suppose I have two classes a base class and an inherited class as follows:

  • 0

Suppose I have two classes a base class and an inherited class as follows:

class Magic : public Attack
{
public:
    Magic(int uID, std::string &name) : Attack(ActionType::MagicAction, uID, name)
    {

    }
};

class MacroMagic : public Magic
{
    MacroMagic(int uID) : Magic(uID, std::string("Testing"))
    {
    }
    void PreUse() override
    {
        std::cout << "Test" << std::endl;
    }
}

I have a shared_ptr to an instance of magic that I would like to copy, but at runtime I will not know whether that pointer points to an instance of Magic, MacroMagic or anything that may have inherited from Magic. I want to be able to copy the object pointed to by the shared_ptr like so:

Battles::magic_ptr mgPtr = MagicNameMap[name];
            if (mgPtr.get() != nullptr)
            {
                return magic_ptr(new Magic(*mgPtr));
            }
            return mgPtr;

where magic_ptr is a typedef for a shared_ptr around the Magic Class. I could do it by specifying a virtual copy function and calling that, but I’d like to make it less obtuse and easier to maintain. I assume I can do this by a copy constructor, but I’m unsure how to in this instance. The way I have it now, the pointer returned by the above code will not call the override pReUse() function.

A bit of guidance would be much appreciated, thanks

  • 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-21T18:08:44+00:00Added an answer on May 21, 2026 at 6:08 pm

    I could do it by specifying a virtual copy function and calling that, but I’d like to make it less obtuse and easier to maintain.

    you’re working against the language.

    you could accomplish what you are after, but i don’t recommend it, and it is certainly not easier to setup or maintain than virtual Magic* clone() = 0.

    perhaps you could outline the problem that brought you to this conclusion, and then we can help from there. there are usually alternatives which don’t fight the language.

    EDIT

    here’s a way around it using an external function table (t_magic_operation_table). you can apply and create several function tables and keep them around. since they exist in the magic object as a single pointer, then you can make these tables quite large (if needed). if your magic types can use the same data/members, then that is one approach. be careful: i threw this together suuuper-fast. it demonstrates the technique, but it’s pretty bad otherwise:

    #include <iostream>
    #include <string>
    
    namespace MONSpiel {
    
    inline unsigned prndm(const unsigned& max) {
        return 1 + arc4random() % max;
    }
    
    class t_ghoul;
    class t_biclops;
    
    class t_magic;
    
    class t_hero {
        t_hero();
        t_hero(const t_hero&);
        t_hero& operator=(const t_hero&);
    public:
        t_hero(const std::string& inName) : d_name(inName) {
        }
    
        const std::string& name() const {
            return this->d_name;
        }
    
        template<typename TEnemy, typename TMagic>
        void attack(TEnemy& enemy, TMagic& magic) const {
            if (enemy.isDead()) {
                return;
            }
    
            enemy.hit(magic.power());
    
            if (enemy.isDead()) {
                std::cout << this->name() << ": see you in the prequel...\n\n";
            }
            else {
                std::cout << this->name() << ": have you had enough " << magic.name() << ", " << enemy.name() << "???\n\n";
            }
        }
    
        /* ... */
    private:
        const std::string d_name;
    };
    
    class t_enemy {
        t_enemy();
        t_enemy(const t_enemy&);
        t_enemy& operator=(const t_enemy&);
    public:
        t_enemy(const std::string& inName) : d_name(inName), d_lifePoints(1000) {
        }
    
        virtual ~t_enemy() {
        }
    
        const std::string& name() const {
            return this->d_name;
        }
    
        bool isDead() const {
            return 0 >= this->d_lifePoints;
        }
    
        const int& lifePoints() const {
            return this->d_lifePoints;
        }
    
        void hit(const int& points) {
            this->d_lifePoints -= points;
        }
    
        /* ... */
    private:
        const std::string d_name;
        int d_lifePoints;
    };
    
    class t_ghoul : public t_enemy {
    public:
        static int MaxDaysAwake() {
            return 100;
        }
    
        t_ghoul(const std::string& inName) : t_enemy(inName), d_bouyancy(prndm(100)), d_proximityToZebra(prndm(100)), d_daysAwake(prndm(MaxDaysAwake())) {
        }
    
        const int& bouyancy() const {
            return this->d_bouyancy;
        }
    
        const int& proximityToZebra() const {
            return this->d_proximityToZebra;
        }
    
        const int& daysAwake() const {
            return this->d_daysAwake;
        }
    
    private:
        int d_bouyancy;
        int d_proximityToZebra;
        int d_daysAwake;
    };
    
    class t_biclops : public t_enemy {
    public:
        t_biclops(const std::string& inName) : t_enemy(inName), d_isTethered(prndm(2)), d_amountOfSunblockApplied(prndm(100)) {
        }
    
        const bool& isTethered() const {
            return this->d_isTethered;
        }
    
        const int& amountOfSunblockApplied() const {
            return this->d_amountOfSunblockApplied;
        }
    
    private:
        bool d_isTethered;
        int d_amountOfSunblockApplied;
    };
    
    class t_magic_operation_table {
    public:
        typedef void (*t_ghoul_skirmish_function)(t_magic&, t_ghoul&);
        typedef void (*t_biclops_skirmish_function)(t_magic&, t_biclops&);
    
        t_magic_operation_table(t_ghoul_skirmish_function ghoulAttack, t_biclops_skirmish_function biclopsAttack) : d_ghoulAttack(ghoulAttack), d_biclopsAttack(biclopsAttack) {
        }
    
        void willSkirmish(t_magic& magic, t_ghoul& ghoul) const {
            this->d_ghoulAttack(magic, ghoul);
        }
    
        void willSkirmish(t_magic& magic, t_biclops& biclops) const {
            this->d_biclopsAttack(magic, biclops);
        }
    
    private:
        t_ghoul_skirmish_function d_ghoulAttack;
        t_biclops_skirmish_function d_biclopsAttack;
    };
    
    class t_action {
    public:
        typedef enum t_type {
            NoAction = 0,
            MagicAction,
            ClubAction,
            ClassAction
        } t_type;
    };
    
    class t_attack {
    public:
        t_attack(const t_action::t_type& actionType, const int& uID, const std::string& inName) : d_actionType(actionType), d_uID(uID), d_name(inName) {
        }
    
        virtual ~t_attack() {
        }
    
        void reset() {
            /* ... */
        }
    
        const std::string& name() const {
            return this->d_name;
        }
    
    private:
        t_action::t_type d_actionType;
        int d_uID;
        std::string d_name;
    };
    
    class t_magic : public t_attack {
        t_magic();
        t_magic(const t_magic&);
        t_magic& operator=(const t_magic&);
    
        static void GhoulSkirmishA(t_magic& magic, t_ghoul& ghoul) {
            magic.d_accuracy = ghoul.bouyancy() + prndm(16);
            magic.d_power = ghoul.proximityToZebra() + prndm(43);
        }
    
        static void GhoulSkirmishB(t_magic& magic, t_ghoul& ghoul) {
            magic.d_accuracy = ghoul.bouyancy() / magic.flammability() + prndm(32);
            magic.d_power = t_ghoul::MaxDaysAwake() - ghoul.daysAwake() + prndm(23);
        }
    
        static void BiclopsSkirmishA(t_magic& magic, t_biclops& biclops) {
            if (biclops.isTethered()) {
                magic.d_accuracy = 90 + prndm(16);
            }
            else {
                magic.d_accuracy = 40 + prndm(11);
            }
    
            magic.d_power = biclops.amountOfSunblockApplied() + prndm(17);
        }
    
        static void BiclopsSkirmishB(t_magic& magic, t_biclops& biclops) {
            if (biclops.isTethered()) {
                magic.d_accuracy = 80 + prndm(80);
            }
            else {
                magic.d_accuracy = 50 + prndm(50);
            }
    
            magic.d_power = 80 + prndm(30);
        }
    
        const t_magic_operation_table* NextOperationTable() {
            static const t_magic_operation_table tables[4] = {
                t_magic_operation_table(GhoulSkirmishA, BiclopsSkirmishA),
                t_magic_operation_table(GhoulSkirmishB, BiclopsSkirmishB),
                t_magic_operation_table(GhoulSkirmishB, BiclopsSkirmishA),
                t_magic_operation_table(GhoulSkirmishA, BiclopsSkirmishB)
            };
    
            return & tables[arc4random() % 4];
        }
    
    public:
        t_magic(const int& uID, const std::string& inName) : t_attack(t_action::MagicAction, uID, inName), d_power(-1), d_accuracy(-1), d_operationTable(0) {
        }
    
        int flammability() const {
            return prndm(73);
        }
    
        int power() const {
            return this->d_power;
        }
    
        void reset() {
            t_attack::reset();
            this->d_power = -1;
            this->d_accuracy = -1;
            this->d_operationTable = 0;
        }
    
    private:
        /* assigns this->d_operationTable */
        void updateOperationTableForAttack() {
            this->d_operationTable = NextOperationTable();
        }
    
    public:
        void heroWillAttack(const t_hero& hero, t_ghoul& ghoul) {
            this->updateOperationTableForAttack();
            this->d_operationTable->willSkirmish(*this, ghoul);
            std::cout << hero.name() << " vs. " << ghoul.name() << "(lp:" << ghoul.lifePoints() << ")";
            this->printState();
        }
    
        void heroWillAttack(const t_hero& hero, t_biclops& biclops) {
            this->updateOperationTableForAttack();
            this->d_operationTable->willSkirmish(*this, biclops);
            std::cout << hero.name() << " vs. " << biclops.name() << "(lp:" << biclops.lifePoints() << ")";
            this->printState();
        }
    
        void printState() {
            std::cout << ": Magic { Power: " << this->d_power << ", Accuracy: " << this->d_accuracy << ", Operation Table: " << this->d_operationTable << "}\n";
        }
    
    private:
        int d_power;
        int d_accuracy;
        const t_magic_operation_table* d_operationTable;
    };
    
    template<typename TEnemy>
    void AttackEnemyWithMagic(t_hero& hero, TEnemy& enemy, t_magic& magic) {
        if (!enemy.isDead()) {
            magic.heroWillAttack(hero, enemy);
            hero.attack(enemy, magic);
            magic.reset();
        }
    }
    
    inline void PlayIt() {
    
        t_hero zoe("Zoe");
        t_hero aragosta("Aragosta");
    
        t_ghoul ghoul0("Al Paca");
        t_ghoul ghoul1("Spud");
        t_ghoul ghoul2("Sleepy");
    
        t_biclops biclops("Scimpanzè");
    
        t_magic hemlock(59, "hemlock");
        t_magic babyPowder(91, "baby powder");
    
        for (size_t idx(0); idx < 1000; ++idx) {
            AttackEnemyWithMagic(zoe, ghoul1, hemlock);
            AttackEnemyWithMagic(aragosta, biclops, babyPowder);
            AttackEnemyWithMagic(zoe, ghoul2, hemlock);
            AttackEnemyWithMagic(aragosta, ghoul0, babyPowder);
        }
    }
    } /* << MONSpiel */
    
    int main(int argc, char* const argv[]) {
    #pragma unused(argc)
    #pragma unused(argv)
        MONSpiel::PlayIt();
        return 0;
    }
    

    another option is to simply create stores (e.g. a vector), each with a different magic type. then fill a vector to point to objects in those stores. that way, you can just create one contiguous allocation for each type and randomize and weigh as needed. this is useful if your magic objects’ sizes vary considerably.

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

Sidebar

Related Questions

I have two classes: class A { public: int i; }; class B :
Suppose we have declared these two classes: public class Animal { //..... } public
Suppose I have two classes in a Rails application: class Subject < ActiveRecord::Base def
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
I have a base class Element and derived two other classes from it, Solid
Suppose I have two classed Base and Derived , i.e.: #include <iostream> class Base
Suppose I have two classes like so: class Parent def say I am a
suppose I have two classes class A extends class B class A has its
Suppose I have two classes: class A(): pass class B(): pass I have another
suppose I have the following class: class MyInteger { private: int n_; public: MyInteger(int

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.