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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:24:38+00:00 2026-06-09T23:24:38+00:00

I want to understand a simple object factory like this. template <class Base> class

  • 0

I want to understand a simple object factory like this.

template <class Base>
class Factory
{
protected:
Factory (std::string name)
...
}
Factory<Base>::Factory (std::string name)
{
        Factory_Map[name] = this;
}
//-------------------------------------
template <class Derived, class Base>
class Factory_Der: public Factory<Base>
{
public:
Factory_Der(std::string name) : Factory<Base>(name) { }
...
};

So the constructor of Factory_Der will call the constructor of Factory. But I don’t know this pointer in constructor of Factory will refer to Factory object or Factory_Der object. It seems that this will refer to Factory object but then the code shouldn’t work! (and it is working).

Thanks

  • 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-09T23:24:39+00:00Added an answer on June 9, 2026 at 11:24 pm

    First of all, you created one object, a Factory_Der object, right? So when you ask, what object does the this pointer point to, well, what object can it point to? You just have one object — a Factory_Der object.

    Since Factory_Der inherits from Factory, part of the memory layout of a Factory_Der object can be viewed as a Factory object. But it’s part of the same object. There is no “separate” Factory object anywhere that it could be pointing to.

    You seemed to get caught up in the fact that these are constructors. But constructors work very much like methods, except that they are special in that they are “automatically called” during object construction after the memory allocation. You could imagine that this object construction:

    Factory_Der *foo = new Factory_Der;
    

    translates underneath into something like this (not real syntax; this is how Objective-C does it however):

    Factory_Der *foo = malloc(sizeof(Factory_Der));
    foo->constructor();
    

    This explains why this in the constructor points to the object.

    At the beginning of every constructor, one can specify an optional initializer list, which specifies how to initialize the base classes and the fields. (If you skip the initializer for something, a default is chosen.) So basically, a constructor automatically calls the constructor of the super class at the very beginning.

    So again, we can imagine that your constructor:

    Factory_Der(std::string name) : Factory<Base>(name) { }
    

    gets translated underneath into something like this:

    void constructor(std::string name) { this->Factory<Base>::constructor(name); }
    

    So you see, the derived class’s constructor essentially “passes” its this to the base class’s constructor. So the object that they see from this is the same object.

    Of course, at the time the base class’s constructor is running, the derived class’s state is not set up. So whether the object can be truly considered a derived class object is a matter of debate. But if your question is whether the object that Factory‘s constructor sees from this is the object that will eventually become a Factory_Der object, then the answer is unequivocally yes.

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

Sidebar

Related Questions

I don't understand why this simple regex match does not return a match object.
I want to understand how I can update my GUI with a simple text
Working on solving a simple problem in C++, want to understand how I could
I understand that Silverlight 3.0 has binding but just want a simple example on
Please help me understand this. I created a really simple program to try to
This seems like a simple exercise, but I'll be darned if I can find
I want to understand the role interface plays in inheritance between two classes. My
I want to understand the actual theory behind types rather than just learning about
I want to understand in which order are the elements of h_addr_list sorted when
I want to understand if code snippets are what I am looking for here.

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.