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

  • Home
  • SEARCH
  • 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 4267304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:52:52+00:00 2026-05-21T06:52:52+00:00

I’ve implemented an undo /redo framework and have a set of command classes that

  • 0

I’ve implemented an undo /redo framework and have a set of command classes that support changing various attributes of the character class. There are several 10+ derived classes each of which is almost exactly the same except for the specific attribute of the character class it operates on.

I would like to push this cookie cutter code into the base class and somehow have the derived class supply the member to operate on. Is this possible?
Note that some of the attributes are bools and some are doubles

class character
{
    public:


    double attribute1;
    double attribute2;
    double attribute3;
    double attribute4;
    //etc

    bool boolattribute1;
    bool boolattribute2;
    //etc

};


class CharacterCommand {    }
class CharacterCommandAttribute1 : public CharacterCommand { }
class CharacterCommandAttribute2 : public CharacterCommand { }
class CharacterCommandAttribute3 : public CharacterCommand { }
class CharacterCommandAttribute4 : public CharacterCommand { }


void CharacterCommandAttribute1::redo()
{
    std::vector<character*> chars = this->textbox->getSelectedCharacters(...);
    for(size_t c=0;c<chars.size();++c)
    {
        character * ch = chars[c];
        ch->attribute1 = this->doValue;
    }
}


void CharacterCommandAttribute1::undo()
{
    std::vector<character*> chars = this->textbox->getSelectedCharacters(...);
    for(size_t c=0;c<chars.size();++c)
    {
        if(c < this->undoValues.size())
        {
            character * ch = chars[c];
            ch->attribute1 = this->undoValues[c];
        }
    }
}

void CharacterCommandAttribute2::redo()
{
    std::vector<character*> chars = this->textbox->getSelectedCharacters(...);
    for(size_t c=0;c<chars.size();++c)
    {
        character * ch = chars[c];
        ch->attribute2 = this->doValue;
    }
}


void CharacterCommandAttribute2::undo()
{
    std::vector<character*> chars = this->textbox->getSelectedCharacters(...);
    for(size_t c=0;c<chars.size();++c)
    {
        if(c < this->undoValues.size())
        {
            character * ch = chars[c];
            ch->attribute2 = this->undoValues[c];
        }
    }
}

Edit
To clarify: Each of the specific command classes is derived from a base class and provide overrides for pure virtual undo and redo functions. These specific classes are stored in a QUndoStack that calls the command’s virtual redo or undo as appropriate.

Each specific command is responsible for the functionality of a single user interface attribute setting for a set of ‘characters’ (the current selection). The specific command stores the existing state of the attribute of each character in a vector and uses these to provide undo state.

Everything works fine; I just feel that there is too much copy & paste repetitive code with only significate difference being the

ch->attribute2 = this->doValue; (refer to ::redo above)

and

ch->attribute2 = this->undoValues[c]; (refer to ::undo above)

Keep in mind that the ‘attribute2’, ‘doValue’, and undoValues vector types are the same within a given derived class but vary between the set of derived classes. This is the detail that prevents me from moving almost everything into the base class.

  • 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-21T06:52:53+00:00Added an answer on May 21, 2026 at 6:52 am

    The solution I came up with seems to work out very well. I’m going to post it for other’s reference and to see the community response. Thanks

    I changed the CharacterCommand base class into a template class:

    template<typename DO_TYPE, typename UNDO_TYPE>
    class CharacterCommand : public TextBoxCommand
    {
    public:
    
    ...
    
    //pure virtual functions that have to be overloaded in the base class
        virtual UNDO_TYPE getUndoValue(xite::Character * ch) = 0;
        virtual void performDo(xite::Character * ch, const DO_TYPE& doValue) = 0;
        virtual void performUndo(xite::Character * ch, const UNDO_TYPE& undoValue) = 0;
    
    protected:
        int startSel;
        int endSel;
    
        std::vector<UNDO_TYPE> undoValues;
        DO_TYPE doValue;
    
    };
    

    An example derived class:

    class CharacterHScale : public CharacterCommand<double,double>
    {
    public:
        ...
    
         double getUndoValue(xite::Character * ch);
        void performDo(xite::Character * ch, const double& doValue);
        void performUndo(xite::Character * ch, const double& undoValue);
    
    ...
    };
    

    In this way the base class is responsible for acquiring and manipulating the set (std::vector) of target characters and the derived class is reponsible for setting / unsetting the single attribute via the virtual performDo and performUndo functions that each derived class must implement.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.