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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:48:15+00:00 2026-06-07T01:48:15+00:00

Following is a template class of Vector which stores different types of data elements.

  • 0

Following is a template class of Vector which stores different types of data elements. check the code of copy constructor and in the main. What I was expecting that the statement “cout << vCHAR2[2] << endl;” should print the value “expense” because copy contractor is doing a shallow copy but it’s
printing “liability”.

Can anybody help me? Thanks.

template<typename T>
class Vector{
  private:
      T* ptr;
      int size;
  public:
      Vector<T>(int s = 10){
           size = s;
           if(size!=0)
           {
               ptr = new T[size];

           }else{
               ptr = 0;
           }

      }
      Vector<T>(const Vector<T> &copy){
            this->size=copy.getSize();

            if(size !=0)
            {
                 ptr=new T[size];
                 for(int i=0;i<size;i++)
                    ptr[i] = copy.ptr[i];    
            }else{
               this->ptr=0;
            }
      }

      ~Vector<T>(){
         if(size>0)
         {
            delete[] ptr;
         }
      }
      int getSize() const
      {
          return size;    
      }
      const Vector<T> & operator = (const Vector<T> &rhs){
            if(this!=&rhs)
                 delete [] this->ptr;
                 size = rhs.size;
                 if(size!=0)
                 {
                      ptr=new T[size];
                      for(int i=0;i<size;i++)
                              ptr[i] = rhs.ptr[i];
            }

            return *this;

      }

      T& operator[](int index){
         if(index>=0 && index<=size)
            return ptr[index];
      }
};    




int main(int argc, char *argv[])
{
  Vector<char*> vCHAR(10);
  vCHAR[0]="asset";
  vCHAR[1]="income";
  vCHAR[2]="liability";

  Vector<char*> vCHAR2(vCHAR);
  vCHAR[2] = "expense";

  cout << vCHAR[2] << endl;

  cout << vCHAR2[2] << endl;

  system("PAUSE");
  return EXIT_SUCCESS;
}
  • 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-07T01:48:16+00:00Added an answer on June 7, 2026 at 1:48 am

    The copy ctor does a deep copy, so vCHAR2 has its own array of elements. So it doesn’t need see when you alter the elements of the source Vector. (it would see it when you altered the data you pointed to via strcpy() or access to vCHAR[2][0]='X'; (provided this wouldn’t crash your programm – as you operate on string literals))

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

Sidebar

Related Questions

I have the following code, which does some iterator arithmetic: template<class Iterator> void Foo(Iterator
My base class, class Foo , is a template class which has the following
Say I have the following template function: template <class T> void apply(const vector<complex<T> >&
In the C++11 standard, Section 23.3.6.2 [vector.cons], the following is said: template <class InputIterator>
I've got the following when building a graph. #include <vector> using namespace std; template<class
I have the following code, which is vital to understand a class I'm working
I created a class which holds data of type and returns an vector on
What is the function of the following C++ template class? I'm after line by
I have the following class: template <typename T> class matrix { private: int _n;
I really like the following template to create properties for class in VS2005 Type

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.