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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:10:19+00:00 2026-06-18T06:10:19+00:00

Note: I have found the issue with how my Xcode was compiling the below

  • 0

Note: I have found the issue with how my Xcode was compiling the below and it appears unrelated to the topic discussed herein. When I have more details I will provide them here.

I recommend voting to close my question as “too localized” since it was an Xcode problem, unrelated to the c++ code itself. Many thanks for the help all the same as I did learn from the answers.

The below question (now answered and resolved) was caused by a confusing exclusion of a file from the Xcode target, thus there were no compiler errors even though the file had problems.


I have a pure virtual interface and want to define its factory method, which returns a subclass of this interface. This works fine:

struct MyVirt{
...all virtual stuff
};

class SubVirt; //forward declaration allows factory:
MyVirt*CreateClass(){
return new SubVirt;
}

Update: Some of the comments say that forward declare is not enough to achieve the above, but that’s not correct. You can accomplish the above fine without the full definition of the SubVirt class.

Now, what I want to do is have a custom constructor that takes arguments. As such:

MyVirt*CreateClass(){
return new SubVirt(arg 1, etc);
}

The problem is that a class forward declaration is no longer sufficient. It needs to see the class definition or its header. This means I can either move the factory method to the file where SubVirt is defined, or I have to include that file in the above file, which creates a circular dependency.

Is there a way to forward declare the custom constructor instead? That would make it all much simpler.

  • 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-18T06:10:20+00:00Added an answer on June 18, 2026 at 6:10 am

    Your CreateClass function looks odd, you miss () in function definition. Should be like this:

    MyVirt* CreateClass()
    {
        return new SubVirt(arg 1, etc);
    }
    

    When return a pointer, compiler needs to know the concrete type and constructor, so forward declare is not enough.

    What you could do is:

    • in header file: forward declare SubVirt and CreateClass function
    • cpp file: include MyVirt.h and define CreateClass function
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PLEASE NOTE, I have found a work around which actually works out better for
Note: I have given up on compiling iPhone Apps without a developer certificate for
Note I have completely re-written my original post to better explain the issue I
We have found an issue in our app where Safari on the Mac randomly
In app engine transactions documentation I have found the following note: Note: If your
I have been researching this issue for a couple of weeks now. Note in
I have an issue with jquery where elements are not found when the query
NOTE: I have read Routing From the Inside Out AND the Engine Yard blog
Note: I have almost no idea at all how batch file 'for' loops work.
NOTE: I have solved the majority of this problem but have run into a

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.