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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:44:28+00:00 2026-06-07T06:44:28+00:00

I’m sort of new to C++ (coming from Java) and I want to declare

  • 0

I’m sort of new to C++ (coming from Java) and I want to declare an array object in a class, but it requires an integer value in its template parameters.
I figured I’d have to make a pointer to an array class, but it does not work..

I want to make something like:

class foo{
    private:
        array *myArray;
    public:
        foo(int size){
            //This line may be terribly wrong, but you see what I mean
            myArray = new array<int,5>(); 
        }
        ~foo(){
            free(myArray);
        }
}

Though, the correct initialization of an array object is:

array<int,5>

but this way does not let me choose length in runtime.

  • 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-07T06:44:30+00:00Added an answer on June 7, 2026 at 6:44 am

    I highly recommend that you pick up a good introductory C++ book, and forget about Java. Seriously, thinking in Java is counterproductive when learning C++. They may have similar syntax but they have very, very different semantics.

    The issues you’re having is related to fundamental concepts of the language. You have to learn the fundamentals from a good C++ introductory book before moving on.


    std::array (if that’s what you’re using) is not the correct class to use for this particular application, because of the fact that you want to choose the length at runtime. The size of the std::array is fixed at compile-time.

    You should use std::vector instead, which allows you to specify (and change) the size at runtime.

    The standard containers such as std::vector manages the memory for you; you don’t need to new or delete the standard containers. The standard containers exist because so you don’t have to manually deal with memory yourself.

    #include <vector>
    
    class foo
    { 
    private: 
        std::vector<int> myArray; 
    public: 
        foo(int size) : myArray(size) // Sets the size of the array
        {
        } 
    
        ~foo()
        { 
            // You don't need to delete anything; the vector takes care of itself.
        } 
    };
    

    Notice that I didn’t use pointers, new, delete, malloc(), or free() anywhere here. You often don’t need pointers for many, many cases in C++. Contrary to popular belief, there’s very little manual memory management that you actually have to do when using modern C++ techniques. In fact, if you’re using delete or free() in your C++ code, you’re probably doing it very wrong.

    I would like to stress again the importance of a good introductory C++ book in assisting you with the language. Any good intro C++ book will cover std::vector and how to use it to your advantage. Other resources such as a std::vector reference can also be of assistance. Familiarize yourself with them.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.