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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:23:11+00:00 2026-05-13T22:23:11+00:00

I am working on a transliteration tool. I have two modules lexer and translator.

  • 0

I am working on a transliteration tool. I have two modules lexer and translator. Lexer generates tokens out of the input text. Depending on the current language chosen, I have to call appropriate translation routine.

I have came up with couple of ideas to do this. First one is to create a base class something called base_translator and provide virtual method (translate()) which every translators has to override. Now create a factory translator_factory and call create() with the language name. This factory will return appropriate instance.

But this seems to be over engineering. So I came up with another approach where I have a struct like the following.

struct translator
{
    const char* name;
    void (*fp)();
};

Which just keeps a language name and a function pointer who can process it. Usage will be,

static translator translators[] = {
    {"first", first},
    {"second", second}
};
const char* language = /* */; 
for(int i = 0; i < 2; i++) {
    translator *t = translators + i;
    if(strcmp(t->name, language) == 0) {
        t->fp();
        break;
    }
}

This approach is very simple and easy to maintain. But I wonder, is this the best approach to the problem? Do you have any suggestions to make this better?

Any help would be great.

  • 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-13T22:23:12+00:00Added an answer on May 13, 2026 at 10:23 pm

    Your second approach might look simpler to you for just two items (well for me it doesn’t…), but it is more error-prone and difficult to maintain in the long run. Whenever you add a new language, you need to touch the code in at least two places. (And trust me: even if this seems unlikely to you at the moment, it is almost inevitably going to happen…) If you forget to update your loop boundary, you have a silent bug in your code. Moreover, this implementation is much slower than a polymorphic one: you need to iterate over the array and compare character strings before each call (as opposed to looking up a pointer in the vtable).

    I would definitely use a factory. Apart from the benefits above, it is a well known design pattern which makes it easier to understand. Thus people coming after you to maintain your code will not curse you that much 😉

    Update: A factory can also return a function pointer. In this case I would still use functors internally, because these are full fledged classes with all the benefits listed above. And one more, not explicitly mentioned: they (as real objects) can store state, which plain functions can’t. That may make a big difference later, and simplify your design a lot. Of course, once you have the factory interface in place, you can easily change your internal representation of its products from plain functions to functors (and back).

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

Sidebar

Related Questions

Working on a project at the moment and we have to implement soft deletion
working on an old translation sample code for windows phone 7. Recently, I have
Working with dates in ruby and rails on windows, I'm having problems with pre-epoch
Working with a SqlCommand in C# I've created a query that contains a IN
Working on a somewhat complex page for configuring customers at work. The setup is
Working on a project that parses a log of events, and then updates a
Working with python interactively, it's sometimes necessary to display a result which is some
Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app
Working with TCL and I'd like to implement something like the Strategy Pattern .
Working with an Oracle 9i database from an ASP.NET 2.0 (VB) application using OLEDB.

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.