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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:34:41+00:00 2026-06-13T22:34:41+00:00

For such case: class A { //implementation }; class B { public: B(); ~B();

  • 0

For such case:

    class A
    {
        //implementation
    };

    class B
    {
     public:
         B();
        ~B();
     private:
         std::vector<std::shared_ptr<A>> _innerArray;
    };

what should I do in the B() to create an object with valid state? Do I need to manually call default constructor for every A object in array? And do I need to do something special in ~B()? If B class is example of bad design, feel free to say how to make it better. Thanks.

Edit
So, here is a scheme of what I really need here.

enter image description here

So real values stored only in array of A and all other objects are for storing connections.
The easiest example – A = dot, B = Line (or curve) going via selected dots and C = a plane described by lines. Hope it makes question more exact.

  • 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-13T22:34:42+00:00Added an answer on June 13, 2026 at 10:34 pm

    To create a B object in a valid state you do not have to do anything more. You even do not have to declare and implement constructor and destructor for B. std::vector<std::shared_ptr<A>> that is a member of B will be default initialized in B‘s constructor which means it will not have any elements in a container yet. It will also be properly deleted in ~B thanks to std::vector and std::shared_ptr destructors.

    On the other hand if you for example want to initialize it somehow (i.e. 3 values) you can use std::vector‘s std::initializer_list constructor in a B‘s constructor initialization list. For example:

    class B
    {
     public:
         B(): _innerArray{ std::make_shared<A>(),
                           std::make_shared<A>(),
                           std::make_shared<A>() } {}
        ~B() {}
     private:
         std::vector<std::shared_ptr<A>> _innerArray;
    };
    

    Remember that std::make_shared uses perfect forwarding so you pass A‘s constructor arguments as the function arguments and not the class object itself.

    Answering your concerns about the design I would like to encourage you to first think about the exclusive ownership of members in a vector before you decide to share them.

    class B
    {
     public:
         B();
        ~B();
     private:
         std::vector<std::unique_ptr<A>> _innerArray;
    };
    

    Above implementation is more effective on many grounds. First of all it makes your design more clear on who is responsible for the lifetime of As. Next std::unique_ptr is faster because it does not demand thread safe reference counting. And last but not least it does not cost any additional memory (compared to regular C pointer) while std::shared_ptr may take tens of bytes (24-48) to store shared state data which is highly ineffective when you operate on small classes. That is why I always use std::unique_ptr as my first resort smart pointer and I only fallback to std::shared_ptr when it is really needed.

    EDIT:

    Answering your edit I would create 3 containers of classes A, B, C. Depending of the fact if you need them to be polymorphic or not I would store either values like that (non-polymorphic types):

    std::deque<A> as;
    std::deque<B> bs;
    std::deque<C> cs;
    

    or (polymorphic types):

    std::vector<std::unique_ptr<A>> as;
    std::vector<std::unique_ptr<B>> bs;
    std::vector<std::unique_ptr<C>> cs;
    

    in that order (as must live longer than bs and bs must live longer than cs). Then I would just have std::vector<A*> inside B class and std::vector<B*> inside C class without any smart pointers usage.

    I hope that helps.

    EDIT:

    Changed std::vector to std::deque in the first case which allows references/pointers to container elements survive containers extensions with push_back(). However they will not survive erasing elements, sorting or other stuff.

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

Sidebar

Related Questions

Consider the (highly simplified) following case: class Dispatcher { public: receive() {/*implementation*/}; // callback
Having such class can I instatiate it in place? abstract class Mammal { public
I have a class, as such: private class verifierListener implements SerialPortEventListener { String outBuffer;
In the following examples: public class RowData { public object[] Values; } public class
To debug multithreaded programs to in case of conditions such as deadlock or livelock,
In the following case, how can I make it such that the text generated
I am quite comfortable with generics and such, but in this special case I
I have a string read from another source such as \b\bfoo\bx. In this case,
In Java, it is very easy to code the following design: public abstract class
Event System handler code: [TcmExtension(My Handler)] public sealed class EventSystem : TcmExtension { public

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.