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

  • Home
  • SEARCH
  • 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 1114045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:56:59+00:00 2026-05-17T02:56:59+00:00

In C++ for Windows, I have some object factory that is supposed to create

  • 0

In C++ for Windows, I have some object factory that is supposed to create a series of Info object by passing a pointer to the object to a Create function and returning a created object.

void CreateInfoObject(AbstractInfo** info);  // The creation function 

AbstractInfo is a base class of which we have many types of Info objects derive.

I thought I could now create an Info object as follows:

MyInfoObject* InfoObj = NULL;  // derived from AbstractInfo object
InfoFactory fc;

fc.CreateInfoObject(&InfoObj); // Now I want to get my initialized pointer back

But it says it cannot do the cast… What is wrong?

ERROR:
Cannot cast from MyInfoObject**_W64 to AbstractInfo**

EDIT: The first answer mentions that the interface is horrid, cannot see who’s allocating etc… How can I improve?

  • 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-17T02:57:00+00:00Added an answer on May 17, 2026 at 2:57 am

    Let’s think about a possible implementation of CreateInfoObject:

    void InfoFactory::CreateInfoObject(AbstractInfo** info)
    {
      *info = new SuperInfo;
    }
    

    Now, SuperInfo and MyInfoObject do not have anything in common right ?

    This is why, in general, the following is forbidden:

    struct Base {};
    struct D1: Base {};
    struct D2: Base {};
    
    int main(int argc, char* argv[])
    {
      Base** base = nullptr;
      D1* d = nullptr;
      base = d;
    }
    

    As it would allow D1 to point to something unrelated.

    There are several solutions:

    // 1. Simple
    AbstractInfo* info = nullptr;
    fc.CreateInfoObject(info);
    
    // 2. Better interface
    std::unique_ptr<AbstractInfo> info = fc.CreateInfoObject();
    

    Then, if you know with certainty that you have, in fact, a MyInfoObject you can use:

    MyInfoObject* myInfo = static_cast<MyInfoObject*>(info);
    

    or if you are unsure:

    MyInfoObject* myInfo = dynamic_cast<MyInfoObject*>(info);
    

    which will set myInfo to nullptr if ever the info did not pointed to an instance of MyInfoObject (or derived).

    Bear in mind though, that your interface is really horrid. It very C-ish and it is unclear whether memory is actually allocated or not… and who is responsible for handling it if it is.

    EDIT:

    In good C++ style, we use RAII to both denote ownership and ensure clean-up. RAII is well-known though not very indicative, I myself prefer the newish SBRM (Scope Bound Resources Management).

    The idea is that instead of using a bare pointer, which does not indicate anything about ownership (ie do you have to call delete on it ?) you should use a smart pointer, like for example unique_ptr.

    You can also make use of the return parameter of the method, to avoid having a two-steps initialization process (first create the pointer, then make it point to an object). Here is a concise example:

    typedef std::unique_ptr<AbstractInfo> AbstractInfoPtr;
    
    // Note: if you know it returns a MyInfoObject
    // you might as well return std::unique_ptr<MyInfoObject>
    AbstractInfoPtr InfoFactory::CreateInfoObject()
    {
      return AbstractInfoPtr(new MyInfoObject());
    }
    
    // Usage:
    int main(int argc, char* argv[])
    {
      InfoFactory factory;
      AbstractInfoPtr info = factory.CreateInfoObject();
    
      // do something
    
    } // info goes out of scope, calling `delete` on its pointee
    

    Here, there is no ambiguity in regard to the ownership.

    Also, note how you better understand your question here:

      std::unique_ptr<MyInfoObject> info = factory.CreateInfoObject();
    

    would not compile because you cannot convert a AbstractInfo* to a MyInfoObject* without using static_cast or dynamic_cast.

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

Sidebar

Related Questions

We have taken over some .NET 1.1 Windows Service code that spawns threads to
I have a Windows Forms application that I wrote that does some monitoring of
For some reason the Windows command prompt is special in that you have to
I have a browser helper object on IE that have some clipboard history functions.
I have a windows Service that start some tasks based on a configuration. Each
I have some windows services written in C#. When somebody stops or starts the
Say I have some windows method and a struct: struct SomeStruct{ int foo; int
I have some problems with Miktex installed on Windows Vista Business SP1/32 bit. I
I am developing a small windows app, but have some trouble deciding whether to
I am running Ruby and MySQL on a Windows box. I have some Ruby

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.