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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:04:00+00:00 2026-05-27T13:04:00+00:00

class example1 { private: int i; public: example1(){i = 1;} int getI(){return i;} };

  • 0
class example1
{
    private:
    int i;
public:
    example1(){i = 1;}
    int getI(){return i;}
};

class example2
{
public:
    example2(){}
    vector<example2> this_vector_wont_compile(3);
    vector <example2> theVec;
    void addVec()
    {
        //what's the scope of this?
        //does push_back create a pointer
        //to which a deep copy of the example1 instance
        //returned by the constructor is performed?
        theVec.push_back(example2());
    }
};
int main()
{
    example2 theExample;
    theExample.theVec[0]; //can be accessed, instance of example1 in scope.
    return 0;
}

Hi, I’m trying to understand the underlying memory operations of using std::vectors. the above example is how i have used them in the past without questioning how its done.

the example2() constructor is returning an instance that goes out of scope when the addVec() function ends, so how does theVec add it whilst keeping it in scope for as long as theVec is?

and also, how come declaring a std::vector as having a constant size within a class produce a compiler error, and how can it be avoided?

  • 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-27T13:04:01+00:00Added an answer on May 27, 2026 at 1:04 pm

    When you call theVec.push_back(example2()); the vector creates a copy of the temporary instance of example2 that is passed in to push_back. This will be done using the copy constructor of the class, which the compiler will automatically generate as you have not explicitly created one.

    I’m not entirely sure what you are asking about declaring a std::vector with a constant size. std::vector by definition does not have a constant size. You can however construct it with an initial size by defining your constructor like this:

    class example2 
    {
        example2() : theVec( 10 ) {};
        std::vector< example2 > theVec;
        ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Public class Example { private int number; public Example(int number){ this.number = number; }
Let's consider this example : public class Shared { private int attribute; public Shared()
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
take a look at this example code: public class Comment { private Comment() {
Earlier I asked why this is considered bad: class Example { public: Example(void); ~Example(void);
If I have a class as follows class Example_Class { private: int x; int
Suppose I've got the following program: namespace ReflectionTest { public class Example { private
In the following class: public class Example<T> where T : IComparable { private T
As an example: public class Foo { private Foo() {} } public class Bar
Example-I have a person class Public Class Person Private _fname As String Public Property

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.