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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:03:59+00:00 2026-05-27T16:03:59+00:00

i have some trobles with the following code: #include <iostream> #incldue <vector> template <typename

  • 0

i have some trobles with the following code:

#include <iostream>
#incldue <vector>

template <typename ElemType>
class A{
private:

  std::vector<ElemType> data;

public:
  A() {};

  A(int capacity) {
    data.reserve(capacity);
  }

  int GetCapacity() {
    return data.capacity();
  }
};

int main() {
  A<int> a;
  a = A<int>(5);
  std::cout << a.GetCapacity() << std::endl; 
} 

The output is 0. What can be the problem?

  • 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-27T16:03:59+00:00Added an answer on May 27, 2026 at 4:03 pm

    The copy-constructor and assignment operator of std::vector<T> are not required to copy the capacity of the vector, only the elements. Because the line a = A<int>(5) indirectly invokes the assignment operator (after creating a temporary), the vector in a does not have a capacity.

    Try changing the first two lines of main to just A<int> a(5) and see what the results are.

    If you absolutely need the capability to transfer the capacity from one instance to another, you need to define the assignment and copy-constructor of A to both copy the data and assign the capacity.

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

Sidebar

Related Questions

I have some troubles with positioning my label/password field. With this code they both
I have some ASP.NET web services which all share a common helper class they
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some classes layed out like this class A { public virtual void
I have some C# / asp.net code I inherited which has a textbox which
I have some code like this in a winforms app I was writing to
I have some troubles with saving a image from memorystream. Here is my code:
I have some troubles with Haskell's type system. Situation: Following program is taking list
I have some HTML that looks like this: <ul class=faq> <li class=open> <a class=question
I start to learn AnyEvent and have some trobles with it. I totally misunderstood

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.