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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:48:58+00:00 2026-05-31T19:48:58+00:00

Summary: In search of the standard C++ design pattern for loading different files via

  • 0

Summary: In search of the standard C++ design pattern for loading different files via constructor

I have a Base class with some functionality that will be used by all derived classes (e.g. Derived_A, Derived_B). The principal difference is that Derived_A and Derived_B override the load function, which is used by the constructor to load a data file (load may also be called explicitly outside the constructor).

I ran into an unexpected problem from this: the load function called by the constructor treats the class as the Base type, but when I use a default constructor and call the load function explicitly, then the virtual function table permits the intended load function to be called.

This smells like a classic problem, but I can’t figure out a way to do it (and I was most recently programming in Python, which I believe, due to weak typing, would always call the intended function).

In the same vein, I’d really like Base::load to be pure virtual / abstract (only derived classes will be instantiated); however, that won’t compile (I believe, because the compiler sees that the pure virtual function will be called).

Can you help?

Output:

Loading w/ constructor:
Base::load file_A
Base::load file_B Loading w/ function post construction:
Derived_A::load file_A
Derived_B::load file_B

Code:

#include <iostream>
#include <string>

class Base
{
public:
  Base() {}
  Base(std::string x)
  {
    load(x);
  }
  virtual void load(std::string x)
  {
    std::cout << "\tBase::load " << x << std::endl;
  }
};

class Derived_A : public Base
{
public:
  Derived_A() {}
  Derived_A(std::string x): Base(x) {}
  void virtual load(std::string x)
  {
    std::cout << "\tDerived_A::load " << x << std::endl;
  }
};

class Derived_B : public Base
{
public:
  Derived_B() {}
  Derived_B(std::string x): Base(x) {}
  void virtual load(std::string x)
  {
    std::cout << "\tDerived_B::load " << x << std::endl;
  }
};

int main()
{
  // simpler code, but it doesn't behave as I hoped
  std::cout << "Loading w/ constructor:" << std::endl;
  Base*der_a = new Derived_A(std::string("file_A"));
  Base*der_b = new Derived_B(std::string("file_B"));

  // this is what I want to do
  std::cout << "Loading w/ function post construction:" << std::endl;
  der_a = new Derived_A;
  der_a->load( std::string("file_A") );
  der_b = new Derived_B;
  der_b->load( std::string("file_B") );
  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-31T19:48:59+00:00Added an answer on May 31, 2026 at 7:48 pm

    The behavior you see is well defined in C++ — it’s just not useful in this scenario because the class is not fully constructed when you call load(std::string) from Base::Base(std::string).

    There are two immediate approaches:

    A

    You could use a container type which calls load (and perhaps holds on to the string as well). This may be more practical if you need to hold on to instances (e.g. they may have specialized error information).

    class Loader 
    {
    public:
        Loader(Base* const p, const std::string& location) : d_base(p) 
        {
            this->d_base->load(location);
        }
    
    private:
        std::unique_ptr<Base>d_base;
    private:
        Loader(const Loader&) = delete;
        Loader& operator=(const Loader&) = delete;  
    };
    

    In use:

    std::cout << "Loading w/ Loader:\n";
    Loader l_der_a(new Derived_A, "file_A");
    Loader l_der_b(new Derived_B, "file_B");
    

    B

    You could also approach it using a helper function:

    class Base {
    public:
        template<typename T>
        static void Load(const std::string& x) 
        {
             T().load(x);
        }
    
        Base() 
        {
        }
    
        Base(std::string x) 
        {
             /* load(x); << see Load(const std::string&) */
        }
    
        virtual ~Base() 
        {
        }
    
        virtual void load(std::string x) = 0;
    };
    

    In use:

    std::cout << "Loading w/ Base::Load<T>():\n";
    Derived_A::Load<Derived_A>("file_A");
    Derived_B::Load<Derived_B>("file_B");
    

    And then there are several other approaches and variations – it depends on what fits your design best. With C++, you certainly have options.

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

Sidebar

Related Questions

I have an abstract base class which inherits Sharp Arch's Entity class: /// <summary>
Summary jquery is used to retrieve search results via the get() call. When rendering
i have noticed a number of empty method and class summary sections throughout a
I have 2 mysql fulltext search queries that return a different set of results.
I am writing some code that uses fixed regexs to search strings and pattern
Summary I am after some advice on the easiest way to analyze simple data
Summary of the problem: For some decimal values, when we convert the type from
Summary A parent can have many children. How do you write a service such
Summary: I'm trying to do classification of some images depending on the angles between
I've to correct the google search title and summary for a website having the

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.