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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:33:44+00:00 2026-06-07T14:33:44+00:00

Lets say that I have multiple functions that accomplish the same goal. An example

  • 0

Lets say that I have multiple functions that accomplish the same goal. An example of this would be creating a ‘constructor’ for a data type.

String new_String();
String new_String(const char *cstr);
String new_String(String s);

Obviously this cant be accomplished in C, but with functions like these (and situations where function overloading is useful) is there a convention or best practice for naming them?

Something like this?

String new_String();
String new_String_c(const char *cstr);
String new_String_s(String s);

which to me feels awkward and not easy to read. Or something like this?

String new_String();
String new_String_from_cstr(const char *str);
String new_String_copy(String s);

Which reminds me of horribly long java names because this could soon get ridiculous.

int String_last_index_of_any_characters(String s, char *chars, int length);
  • 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-07T14:33:47+00:00Added an answer on June 7, 2026 at 2:33 pm

    You can define a single “constructor” that takes a void * initialization parameter and an enum:

    enum StringCtor { SC_DEFAULT, SC_C_STR, SC_COPY };
    
    String new_String(enum StringCtor type, const void *arg);
    
    String s1 = new_String(SC_DEFAULT, 0);
    String s2 = new_String(SC_C_STR, "hello");
    String s3 = new_String(SC_COPY, &s2);
    

    You could also choose to use a ... instead of a void *. The idea is the same, but you use the va_* macros to extract the parameter if it supposed to be a string or a copy.

    If you just want the API to have the appearance of a single constructor, but still want type safety, you can use the techniques above to create your actual constructor implementation, and use inline functions and preprocessor tricks to give the appearance of a single constructor, with type safety.

    String new_StringImpl(enum StringCtor type, const void *arg);
    
    static inline String new_StringImplDefault () {
        return new_StringImpl(SC_DEFAULT, 0);
    }
    static inline String new_StringImplCstr (const char *s) {
        return new_StringImpl(SC_C_STR, s);
    }
    static inline String new_StringImplCopy (String *s) {
        return new_StringImpl(SC_COPY, s);
    }
    
    #define new_String_Paste(TYPE) new_String_ ## TYPE
    #define new_String_SC_DEFAULT(ARG) new_StringImplDefault()
    #define new_String_SC_C_STR(ARG) new_StringImplCstr(ARG)
    #define new_String_SC_COPY(ARG) new_StringImplCopy(ARG)
    
    #define new_String(TYPE, ...) new_String_Paste(TYPE)(__VA_ARGS__)
    
    String s1 = new_String(SC_DEFAULT);
    String s2 = new_String(SC_C_STR, "hello");
    String s3 = new_String(SC_COPY, &s2);
    

    Notice with the variadic macro, SC_DEFAULT does not need a second parameter any more. At sufficient optimization levels, the code translates to just a call to the single implementation function, with the benefit of compile time type safety checks. So at the cost of some more coding on your part, you can give the user of your library the appearance of a single constructor API with all the type safety of multiple constructor functions.

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

Sidebar

Related Questions

Lets say there are multiple functions throughout my program that need to append data
Let's say you have multiple directories that all exist on the same partition. Let's
Let's say I have a data mapper function that aggregates multiple tables and generates
Let's say I have a program(C++, for example) that allocates multiple objects, never bigger
Let's say that I have a multiple select field like this: <select id=foodlist multiple=multiple>
Lets say that i have 2 pages: index.php and service.php index.php sends an http-post
Lets say that you have a following simple application: <form action=helloServlet method=post> Give name:<input
Lets say that i have a duration of time e.g., 10 minutes . I
Lets say that you have a business object whose current state implies that there
Lets say that I have an executable and when it is started I want

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.