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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:06:33+00:00 2026-05-25T20:06:33+00:00

I tried to create a list container with similar access of elements in c++

  • 0

I tried to create a list container with similar access of elements in c++ as in C#
I’m totally lost now because my main method first printed weird numbers.

The RList class should be like:

RList<ClassName or Primitive> VariableName;

VariableName.AddData(Class or Primitive);

VariableName[IndexOfElement] get the element

VariableName.RemoveAt(IndexOfElement) remove element

Can you tell me where I went totally wrong?

int main()
{

RList<int> Numbers;

    Numbers.AddData(5);
    Numbers.AddData(100);
    Numbers.AddData(1500);

for (unsigned int x = 0; x < Numbers.GetLength(); x++)
{

    cout << Numbers[0] << endl;
}



cin.get();

return 0;
}

Here is the Header file. I read that you have to put everything in header if you work with template.

#ifndef RList_H
#define RList_H

#include <new>


template <class T> class RList
{

 private:
    unsigned int m_Length;
    T* ListObject;

    void AllocateNew(T obj);
    void RemoveIndex(unsigned int N);

public:
    RList();
    ~RList();
    void AddData(T obj);
    void RemoveAt(unsigned int N);
    unsigned int GetLength() { return m_Length; }
    T operator[](unsigned int N){if (N < m_Length && N >= 0) {return (ListObject[N]);} return NULL; }


};

template <class T>
RList<T>::RList()
{

this->m_Length = 0;


}

template <class T>
RList<T>::~RList()
{

delete[] this->ListObject;


}

template <class T>
void RList<T>::AddData(T obj)
{
this->AllocateNew(obj);
this->m_Length++;

}

template <class T>
void RList<T>::RemoveAt(unsigned int N)
{
if( N < this->m_Length && N >= 0)
{
    if ((this->m_Length - 1) > 0)
    {
            this->RemoveIndex(N);
            this->m_Length--;
    }

    else
    {

    throw "Can't erase last index!";



    }


}



}

template <class T>
void RList<T>::AllocateNew(T obj)
{


if (this->m_Length == 0)
{
    this->ListObject[0] = obj;

}

else
{

T* NewListObject = new T [this->m_Length + 1];

for (unsigned int x = 0; x < this->m_Length; x++)
{
    NewListObject[x] = this->ListObject[x];

}

NewListObject[this->m_Length] = obj;

delete [] ListObject;
this->ListObject = NewListObject;

delete [] NewListObject;

}

}


template <class T>
void RList<T>::RemoveIndex(unsigned int N)
{
T* NewListObject = new T [this->m_Length - 1];
for (int x = 0; x < this->m_Length -1; x++)
{
    if (x != N)
    {

        NewListObject[x] = this->ListObject[x];

    }


}
delete [] ListObject;
this->ListObject = NewListObject;


}
#endif // RList_H
  • 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-25T20:06:34+00:00Added an answer on May 25, 2026 at 8:06 pm

    Lots of problems:

    1. Constructors should initialize all members
    2. Rule of three not implemented (on owned pointers).
    3. Horrible spacing (you need to format that code better).
    4. All your array memory allocation is wrong.

    Allocate:

    template <class T>
    void RList<T>::AllocateNew(T obj)
    {
        if (this->m_Length == 0)
        {
            // This will not work as you have not allocated the area for ListObjects.
            // I don't think this is a special case. You should have allocated a zero
            // length array in the constructor then then else part would have worked
            // like normal when adding the first element.
            this->ListObject[0] = obj;
        }
        else
        {
            // OK good start
            T* NewListObject = new T [this->m_Length + 1];
    
            // Rather than do this manually there is std::copy
            for (unsigned int x = 0; x < this->m_Length; x++)
            {
                NewListObject[x] = this->ListObject[x];
            }
            NewListObject[this->m_Length] = obj;
    
           // Though unlikely there is a posability of an exception from a destructor.
           // So rather than call delete on a member you should swap the member and the
           // temporary. Then when the object is a good state you can delete the old one.
           delete [] ListObject;
           this->ListObject = NewListObject;
    
           // Definately do NOT do this.
           // as you have just stored this pointer into ListObject.
           // ListObject is now pointing at free'ed memory.
           delete [] NewListObject;    
    
           // So I would have done (for the last section
           //   std::swap(this->ListObject, NewListObject);
           //   ++this->m_Length;
           //   // now we delete the old data
           //   delete [] NewListObject; // (remember we swapped above)
        }
    }
    

    RemoveIndex

    template <class T>
    void RList<T>::RemoveIndex(unsigned int N)
    {
        T* NewListObject = new T [this->m_Length - 1];
        for (int x = 0; x < this->m_Length -1; x++)
        {
            if (x != N)
            {
                // You need to compensate for the fact that you removed one
                // element (otherwise you have a hole in your new array).
                NewListObject[x] = this->ListObject[x];
            }
        }
    
        // Same comment as above.
        // Do not call delete on a member.
        // Make sure the object is a good state before doing dangerous stuff.
        delete [] ListObject;
        this->ListObject = NewListObject;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to create a script to list the contents of a directory: #!/bin/bash
I tried to create a new folder following the instructions at https://developers.google.com/google-apps/documents-list/#creating_a_new_document_or_file_with_metadata_only instead of
I tried to create a single insert query for the values in template_list array.
I created a linked list and when I tried to print values of the
I have tried create this table, but nothing I have tried works from FKs.
Tried to create a mobile navigation using dataview or outline and the view renders
I tried to create base controller and write in it: Ext.define('My.Users.controller.Role', { extend :
I tried to create jar file using command line that I inserted into java
I tried to create a code that adds 2 percent each time a component
I tried to create dynamic object to validate my config in fly and present

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.