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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:15:43+00:00 2026-05-23T15:15:43+00:00

CLOS has a neat concept of :before, :after, and :around methods. The :before methods

  • 0

CLOS has a neat concept of :before, :after, and :around methods.

  • The :before methods are called before the primary method.
  • The :after methods are called after the primary method.
  • The :around methods are called around the :before+primary+:after sequence.

The :before, :after, and :around methods are chained rather than overridden. Suppose both a parent and child class define a foo method and :before foo method. The child’s foo method overrides the parent’s foo method, but both the child’s and parent’s :before foo methods are called before this overridden method is called.

Python decorators provide something similar to the CLOS :around methods. There is nothing like this in C++. It has to be hand-rolled:

class Child : public Parent {
    virtual void do_something (Elided arguments) {
        do_some_preliminary_stuff();
        Parent::do_something (arguments);
        do_some_followup_stuff();
    }
};

Downsides:

  • This is an anti-pattern to some people.
  • It requires me to be explicit in specifying the parent class.
  • It requires extenders of my classes to follow the same paradigm.
  • What if I need to call the grandparent because the parent doesn’t override do_something, and what about multiple inheritance?
  • It doesn’t quite capture the CLOS concept.

I found these concepts to be quite handy way back when I used Flavors (predecessor to CLOS). I’ve used the above workaround in a few places, and a few have challenged it as an anti-pattern. (Others have emulated it elsewhere, so the derision is not universal.)

The question: Is this considered an anti-pattern in C++, and are there better workarounds?

  • 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-23T15:15:43+00:00Added an answer on May 23, 2026 at 3:15 pm

    This is what I could do, but it’s still a bit ugly.

    Basically I put the actual work in a separate hook so you don’t have call the pre/post hooks in the processing method. In the inheritance chain you have total control both on whether you want to add pre/post hooks and the ordering of the hook calls (call the parent’s hook before or after the child’s hook).

    #include <iostream>
    #define C(s) std::cout << s << std::endl;
    
    class Parent {
        public:
        virtual void do_something(int arg) {
            do_some_pre_hook();
            do_some_hook(arg);
            do_some_post_hook();
        }
        virtual void do_some_pre_hook() {
            C("parent pre_hook");
        }
        virtual void do_some_post_hook() {
            C("parent post_hook");
        }
        virtual void do_some_hook(int arg) {
            //this is where you actually do the work
        }
    };
    
    class Child : public Parent {
        public:
        typedef Parent super;
    
        virtual void do_some_pre_hook() {
            super::do_some_pre_hook();
            C("Child pre_hook");
        }
        virtual void do_some_post_hook() {
            super::do_some_post_hook();
            C("Child post_hook");
        }
    };
    
    class GrandChild : public Child {
        public:
        typedef Child super;
    
        virtual void do_some_pre_hook() {
            super::do_some_pre_hook();
            C("GrandChild pre_hook");
        }
        virtual void do_some_post_hook() {
            super::do_some_post_hook();
            C("GrandChild post_hook");
        }
        virtual void do_some_hook(int arg) {
            //this is where you actually do the work
            C("GrandChild hook");
        }
    };
    
    int main() {
        GrandChild gc;
        gc.do_something(12);
    }
    

    Note: Ideally you would use an AOP c++ compiler or compiler extension for a task like that, but the last time I tried it it wasn’t quite stable…

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

Sidebar

Related Questions

I'm writing some methods to emit HTML for various elements. Each method has the
How do I see if one CLOS class is a subclass of another CLOS
If you want to make CLOS objects in common lisp printable (print readably), how
I can define an object and assign attributes and methods: class object: def __init__(self,a,b):
I'm running up against a problem in understanding the CLOS way of handling file
How would you deal with times and after midnight in PHP / MySQL? Use
Could any CL'er please explain 'slots' in CLOS? I am finding it difficult to
I am building a mechanism to take an arbitrary CLOS object and return a
In the following code, can i remove the :after ? (defmethod render-page-body :after ((app
In Common-Lisp CLOS Is it possible to dynamically add one more super class in

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.