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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:33:21+00:00 2026-06-08T19:33:21+00:00

The constructor of class StructComponent takes a different logic to initialize its member variables

  • 0

The constructor of class StructComponent takes a different logic to initialize its member variables based on the type of the pass-in object of info. Here I use casting to convert the pass-in parameter to right subclass object.

class StructComponent
{
public:
    StructComponent(const ClassA& info)
    {
        if (info.getType() == CLASS_B)
        {
            const ClassC& classC = dynamic_cast<const ClassC&> info;
            ...
            apply a different logic for ClassB and init member accordingly
        } else if (info.getType() == CLASS_C) {
            apply a different logic for others
            ...
        } else {
                    apply default
            }
    }
}

class ClassA
{
public:
    ClassA(...)
    {
        m_shp = CreateStructComponent();
    }   
    virtual boost::shared_ptr<StructComponent> CreateStructComponent()
    {
        return boost::shared_ptr<StructComponent> (new StructComponent(*this));     
    }

    ...
    int getType() const { return CLASS_A; }

protected:
    boost::shared_ptr<StructComponent> m_shp;
}

class ClassB : public ClassA
{
public:
    ...

    virtual boost::shared_ptr<StructComponent> CreateStructComponent()
    {
        return boost::shared_ptr<StructComponent> (new StructComponent(*this));     
    }   
    ...
    int getType() const { return CLASS_B; } 
}

class ClassC : public ClassA
{
public:
    ...

    virtual boost::shared_ptr<StructComponent> CreateStructComponent()
    {
        return boost::shared_ptr<StructComponent> (new StructComponent(*this));     
    }   
    ...
    int getType() const { return CLASS_C; } 
}

Q1> Is the code correct ignore the potential design issue?

Q2> Assuem all subclass of ClassA has the same implementation body of the function CreateStructComponent.
Is there a way that I can save space not to repeatedly do the same code as follows:

return boost::shared_ptr<StructComponent> (new StructComponent(*this));

Q3> Is there a better design that I can use? For example, is there a way that I can ignore casting in the
StructComponent?

  • 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-08T19:33:23+00:00Added an answer on June 8, 2026 at 7:33 pm

    1) No, it’s not correct, at least, it doesn’t do what you probably expect. It calls a virtual function in the constructor of ClassA, which will always call ClassA::CreateStructComponent() instead of calling the overriding function in the derived class, because when the ClassA constructor runs that dynamic type is ClassA. For the same reason, in the constructor for StructComponent the getType() call will always resolve to ClassA::getType().

    2) There are lots of ways to solve that problem. You could put the code in a template, so it depends on the type, or you could just get rid of the need to duplicate the code, by doing the initialization in a different place.

    3) Why not simply give StructComponent overloaded constructors, one taking ClassB, one taking ClassC and another taking ClassA?

    Then again, a better solution would probably be to get rid of the StructComponent constructor and have ClassA, ClassB, or ClassC do the initialization explicitly, so that each type initializes it how it wants it done. If the initialization depends on the type that creates it, the initialization doesn’t belong in the StructComponent constructor. Currently you have a circular dependency, StructComponent needs to know about all the types that use it, and all the types that use it need to know about StructComponent. That’s usually a sign of a problem with the design, all the classes are tightly coupled to each other. It would be better if StrictComponent knew nothing about the other types.

    Anyway, here’s one possible solution, showing a way to pass the correct type to the StructComponent without duplicating code, but I don’t think this is a good design. Note that the Structcomponent constructor is passed NULL, so it can do different things based on the type, but cannot access the objects it is passed.

    class StructComponent
    {
    public:
        explicit StructComponent(const ClassA*)
        { /* something */ }
    
        explicit StructComponent(const ClassB*)
        { /* something else */ }
    
        explicit StructComponent(const ClassC*)
        { /* something completely different */ }
    };
    
    class Base
    {
    protected:
      template<typename T>
        explicit
        Base(const T*)
        : m_shp( boost::make_shared<StructComponent>((T*)NULL) )
        { }
    
        boost::shared_ptr<StructComponent> m_shp;
    };
    
    class ClassA : virtual public Base
    {
    public:
        ClassA() : Base(this) { }
    };
    
    class ClassB : public ClassA
    {
    public:
        ClassB() : Base(this) { }
    };
    
    class ClassC : public ClassA
    {
    public:
        ClassC() : Base(this) { }
    };
    

    Here’s another, completely-different approach without virtual base hacks, still removing duplicated code, and allowing StructComponent to access the objects passed to it (which I maintain is a bad idea):

    class StructComponent
    {
    public:
        explicit StructComponent(const ClassA& a)
        { /* something */ }
    
        explicit StructComponent(const ClassB& b)
        { /* something else */ }
    
        explicit StructComponent(const ClassC& c)
        { /* something completely different */ }
    };
    
    class ClassA
    {
    public:
        ClassA() : m_shp( create(*this) ) { }
    
    protected:
        struct no_init { };
    
        explicit
        ClassA(no_init)
        : m_shp()
        { }
    
        template<typename T>
          boost::shared_ptr<StructComponent> create(const T& t)
          { return boost::make_shared<StructComponent>(t); }
    
        boost::shared_ptr<StructComponent> m_shp;
    };
    
    class ClassB : public ClassA
    {
    public:
        ClassB()
        : ClassA(no_init())
        { m_shp = create(*this); }
    };
    
    class ClassC : public ClassA
    {
    public:
        ClassC()
        : ClassA(no_init())
        { m_shp = create(*this); }
    };
    

    And here’s yet another choice, this time without the circular dependencies, moving the different initialization code where it belongs:

    struct StructComponent
    {
      StructComponent() { /* minimum init */ }
    };
    
    class ClassA
    {
    public:
        ClassA() : m_shp( createA() ) { }
    
    protected:
        struct no_init { };
    
        explicit
        ClassA(no_init)
        : m_shp()
        { }
    
        boost::shared_ptr<StructComponent> createA()
        {
          // something
        }
    
        boost::shared_ptr<StructComponent> m_shp;
    };
    
    class ClassB : public ClassA
    {
    public:
        ClassB()
        : ClassA(no_init())
        { m_shp = createB(); }
    
    private:
        boost::shared_ptr<StructComponent> createB()
        {
          // something else
        }
    };
    
    class ClassC : public ClassA
    {
    public:
        ClassC()
        : ClassA(no_init())
        { m_shp = createC(); }
    
    private:
        boost::shared_ptr<StructComponent> createC()
        {
          // something completely different
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class constructor that expects a reference to another class object to
I am not following how to initialize a class constructor which accepts the names
I have an object of class which has private constructor: class CL_GUIComponent { //
I'm extending a constructor class' functionality via the prototype method, but I'm having trouble
Into a class constructor, I need to create some objects on the fly and
I have a class constructor like this: public JavoImageCorrectedDataHeader() { ByteBuffer buffer = ByteBuffer.allocate(this.size());
When you define a class constructor in a base class (i.e. to set some
I'm redesigning a class constructor in C++ and need it to catch an unspecified
So I can't use initializers in my class constructor because of using arrays, so
Possible Duplicate: Calling virtual method in base class constructor Calling virtual functions inside constructors

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.