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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:35:44+00:00 2026-05-20T00:35:44+00:00

Rewrote the question completely. Please, read it carefully Single note to not confuse you:

  • 0

Rewrote the question completely. Please, read it carefully

Single note to not confuse you: Base constructor expects pointer to constants array. It doesn’t store a pointer itself, it stores the data!

I have the following code:

class Base {
public:
    Base(int*);
    // added this to explain why I need inheritance
    virtual void abstractMethod() = 0;
};

Base::Base(const int *array) {
    // just for example
    cout << array[0] << endl;
    cout << array[1] << endl;
    cout << array[2] << endl;
}

class Derived : private Base {
public:
    Derived();
    void abstractMethod();
};

// who will delete? how to initialize?
Derived::Derived(): Base(new int[3]) {
}

I want to hide Base(int*) constructor from the user of my Derived class. To do that I need to supply default values to that array.

The problem is that when I use initialization list like this:

Derived::Derived(): Base(new int[3]) {
}

array is not initialized and Base constructor prints some garbage.
Another problem with this code: who will free that new array?

How to initialize array before it is passed to Base class?
Is it possible at all in C++?

  • 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-20T00:35:45+00:00Added an answer on May 20, 2026 at 12:35 am

    I came into another solution:

    class Int3Array {
        int array[3];
    public:
        Int3Array(int v1, int v2, int v3) {
            array[0] = v1;
            array[1] = v2;
            array[2] = v3;
        }
        int* getPtr() {
            return array;
        }
    };
    
    Derived::Derived(): Base((Int3Array(1,1,1)).getPtr()) {
    }
    

    What do you think? Is it also bad?

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

Sidebar

Related Questions

Completely rewrote the question as I now have more information about what is happening.
UPDATE (2010-12-21): Completely rewrote this question based on tests that I've been doing. Also,
Edit - Rewrote question since the original did not makes sense: In R -
Please, move this question to Code Review -area . It is better suited there
I am not sure the title of this question covers what I mean. In
Rewrote the entire question because evidently it was too vague I have HTML, this
I know some of you just feel it is as a completely wrong question
I'm revising my question from a few days ago. Rewrote my query. Can someone
I have a simple mod_rewrite question. What do I need to write in the
I thought I would rewrite this question (same iteration). The original was how to

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.