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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:37:38+00:00 2026-05-20T00:37:38+00:00

Last year I saw some source code (C++) where its author declares static function

  • 0

Last year I saw some source code (C++) where its author declares static function in base class, but leaves its definition to derived class. I remember there was constraint that only one derived class was permitted to define aforementioned static function.

I know that it is impossible to override static methods, but this trick is exactly what I need. I just can’t make it work in my code 🙂 Does anyone know about this feature?

Lets look why this would be useful. Suppose we have some base class (Shape), and its derived classes (Circle, Triangle…). Suppose Shape is part of my core architecture, and derived classes are treated as plugins. I don’t want to change my core architecture in future. So we have:

class Shape
{
    //other stuff here
    static Shape* Factory();
}

class Circle:Shape
{
    //other stuff here
    static Shape* Factory();
}

Shape is sort of abstract class, and it will not implement Factory method. Method is implemented by one (and only one) of the derived classes. In implementation derived class will return new instance of itself, so it is just a factory method. This trick allowed its author to use this static method in client class in following way:

class Client
{
    public Shape* shape;
    public Client();
    //other stuff here
}

In implementation of constructor he had something like:

Client::Client()
:shape(Shape::Factory())
{        
}

This way he was able to instantiate “right” shape derivation without changing core classes in engine. When he wanted some other shape to be used in core classes he just had to define static Factory method in that derived class (and to remove the existing one in other derived class).

This way we have some sort of “static polymorphism”. I can’t find anything about this technique on the web. Does it even have a name? I am especially interested if something like this could be achieved in C# language? 🙂

Thanks in advance, and sorry for my bad English.

  • 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-20T00:37:39+00:00Added an answer on May 20, 2026 at 12:37 am

    What it sounds like you are trying to do is a bit messy in my opinion. It feels like a combination of a Factory class, a Singleton and then trying to squish them all back into your result class hierarchy.

    The simplest (not necessarily the best) solution I can think of is forget about having either Circle::Factory() or Shape::Factory() and just have a free function called get_default_shape().

    class Shape
    {
    };
    
    class Circle: public Shape
    {
    };
    
    Shape * get_default_shape()
    {
      return new Circle;
    }
    
    Client::Client()
    :shape(get_default_shape())
    {
    }
    

    The nice bit about this is that its only the implementation of get_default_shape that needs to include Circle.h, all the definition needs is a forward declaration of the Shape class.

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

Sidebar

Related Questions

In the last year and a bit of working on my team's code base
How can I get last year's start and end date using PHP code? Is
Last year I received two Technical Support Incidents in the iPhone developer program, but
Last year (2009), the Google Code Jam featured an interesting problem as the first
I've spent the last year learning and using Java in university but we didn't
I'm a student, have a test tomorrow, and I saw that last year there
Within the last year, I've been working with other people on some Objective-C projects
A question exactly like this was asked last year, but this question was for
i have a class which queries for sales for last year and updates a
First a little intro: Last year i wrote this http://dragan.yourtree.org/code/canvas-3d-graph/ Now, i want to

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.