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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:50:21+00:00 2026-05-26T06:50:21+00:00

I am converting a bunch of code from C++ in to C. Is there

  • 0

I am converting a bunch of code from C++ in to C. Is there an equivalent pattern in C for a Object factory?

Consider the following source code. Based on a parameters (int type) the ObjectFactory() function should return a void pointer to an struct of a pedicure type. How can I instantiate the struct in a way that I can have a pointer to it after the function returns.

typedef struct {
    unsigned int a; 
    unsigned int b; 
    unsigned int c; 
} CThings ; 

typedef struct {
    unsigned int d; 
    unsigned int e; 
    unsigned int f; 
} CPlaces ; 

void * ObjectFactory( int type ) {

    switch( type ) {
        case 5 : {          
            return ??? CPlaces ; 
            break; 
        }
        case 35 : {         
            return ??? CThings ; 
            break; 
        }
        default: {
            // unknown type 
            return NULL ; 
        }
    }

    return NULL ; 
}



int _tmain(int argc, _TCHAR* argv[])
{
    void * p = ObjectFactory( 5 ); 

    // Do soemthing with the pointer. 
    CPlaces * places = (CPlaces*) p ; 

    places->d = 5 ; 
    places->e = 6 ; 
    places->f = 7 ; 


    return 0;
}
  • 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-26T06:50:22+00:00Added an answer on May 26, 2026 at 6:50 am

    How about using malloc:

    case 5: return malloc(sizeof(struct CPlaces));
    

    No need for the break if you already return. If you like, you can add some initialization before returning.

    The caller will have to know the actual type so she can cast the pointer back to the correct type. This will probably amount to a duplicate switch statement at the caller’s site.

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

Sidebar

Related Questions

Is there any way to extract hyperlinks from .doc. I got bunch of hyperlinks
I've been converting a bunch of files from Coldfusion to C#, and all has
I'm converting a image to gray scale in Java with the following code: BufferedImage
I've been converting a bunch of old C++ code into C++/CLI code, and I
I'm given a task of converting a bunch of code written in Python 2
Converting my current code project to TDD, I've noticed something. class Foo { public
Converting a couple stored procedures from MySQL to Microsoft SQL server. Everything is going
Converting to ODB.NET from System.Data.OracleClient and need help converting my connection string. Here is
I am converting from existing CVS repository to SVN repository. CVS repository has few
I'm converting an application to use Java 1.5 and have found the following method:

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.