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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:42:48+00:00 2026-06-11T11:42:48+00:00

I have written a macro that can be added around a class definition that

  • 0

I have written a macro that can be added around a class definition that will effectively final-ize it (prevent subclassing). This method of finalization is a fairly common technique and is noted in the C++ FAQ:

#define FINALIZE(NAME,...) \
    NAME##Finalizer {\
        private:\
            friend class NAME;\
            inline NAME##Finalizer(void) {}\
    };\
    class NAME : public virtual NAME##Finalizer, __VA_ARGS__

For example:

class FINALIZE(Fred) {};
//The above line evaluates to:
//  class FredFinalizer {
//      private:
//          friend class Fred;
//          inline FredFinalizer(void) {}
//  };
//  class Fred : public virtual FredFinalizer, {};

class George {};
class FINALIZE(Fred2, public George) {};
//The above line evaluates to:
//  class Fred2Finalizer {
//      private:
//          friend class Fred2;
//          inline Fred2Finalizer(void) {}
//  };
//  class Fred2 : public virtual Fred2Finalizer, public George {};

class Velma : public Fred { Velma(void) {} /*Note compile error*/ };
class Velma2 : public Fred2 { Velma2(void) {} /*Note compile error*/ };

This works splendidly. The problem is that I would now like a way to “disable” the macro. That is:

class FINALIZE(Fred) {};
//The above line evaluates to:
//  class Fred {};

class George {};
class FINALIZE(Fred2, public George) {};
//The above line evaluates to:
//  class Fred2 : public George {};

The issue is that the class Fred2 : public George {}; clashes with the case where there’s no inheritance (class Fred {};): in the first case, there must always be a colon, in the second case, there mustn’t. Note that this isn’t an issue when the macro is enabled because the class always inherits from at least the NAME##Finalizer class.

How can I write a macro that does this?

  • 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-06-11T11:42:49+00:00Added an answer on June 11, 2026 at 11:42 am

    I’d advice to make two macros, one for preparing finalizer, one for using:

    #define PREP_FINALIZE(NAME) \
        class NAME##Finalizer {\
            private:\
                friend class NAME;\
                inline NAME##Finalizer() {}\
        }
    
    #define FINALIZE(NAME) virtual NAME##Finalizer
    

    So in your example:

    PREP_FINALIZE(Fred);
    class Fred : FINALIZE(Fred) {};
    
    class George {};
    PREP_FINALIZE(Fred2);
    class Fred2 : FINALIZE(Fred2), public George {};
    

    But what is more important you forget about to make copy constructor private. Without it your way can be cheating very easily:

    // cheating :D
    class Velma : public Fred, FINALIZE(Fred) { 
       Velma() : FredFinalizer(*(FredFinalizer*)0) {} /*Who said this is compiler error */ 
    };
    

    Make copy constructor of finalizer private. Or use my answer to very similar question.

    For C++11 just use final 😉

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

Sidebar

Related Questions

I have written a VBA macro in an excel spreadsheet that opens all workbooks
I have written function (actually a macro, but whatever) that works similarly to progn
I have written a simple ASP.NET MVC 2 application that stores data and can
I have written a macro that prints HTML output with passed value. Let's say:
I have an HTML report that can be opened in Excel. I have written
I have written a macro that searches through workbook and applies an autofilter to
I have written a macro that to return specific values in one cell based
I have recently written a macro for visual studio 2010 in the macro IDE,
have written this little class, which generates a UUID every time an object of
I have written a function that sorts a big scale of data. To test

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.