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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:41:42+00:00 2026-05-11T00:41:42+00:00

I am just starting with C++ and got some problems in understanding how the

  • 0

I am just starting with C++ and got some problems in understanding how the scope for private member variables in a class works. Please see the below code

class Foo{     private:         std::vector<int> container;     public:         // other methods };  int main(int argc, char* argv[]) {     Foo* foo = new Foo;     // other method calls to which foo is passed     delete foo;     return 0; } 

In the above code, variable ‘container’ is a private member variable. I am invoking ‘Foo’ instance and passing it to several other methods and classes. Following are my doubts

  1. What will be the scope of variable ‘container’? Will that variable exist until I delete the instance foo?
  2. Do I need to make the ‘container’ as a pointer to vector?

Thanks for the help

  • 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. 2026-05-11T00:41:43+00:00Added an answer on May 11, 2026 at 12:41 am
    1. Yes, the lifetime of the container member will last as long as the object that contains it exists, which is until you call delete on the pointer pointing to it (foo in your case).
    2. No, there is no reason to do that. Making it a pointer requires you to create a dynamic object of vector<int> which you would need to manage the lifetime of (including calling delete on the container pointer). That’s unnecessary here. Assuming you want the container last as long as the Foo object, you are fine with having it directly contained, without doing it with a pointer.

    Passing the foo pointer will just pass the pointer. The object that it points to will not be copied, only the pointer pointing to it if necassary. If you know Java, then it helps you if i tell you passing the pointer is the same as just passing a reference to an object in Java, say:

    Foo f = new Foo(); // just passes the reference (pointer in C++) to doIt.  // the actual object is not copied doIt(f); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just starting with Modelica and having trouble understanding how it works. In the below
I'm just starting a new GAE project, and I see they've done some sprucing
Here are some sample starting values for variables in the code below. sd <-
I am just starting with Sphinx. So far I got it installed successfully, got
i'm just starting with the mvvm model in Silverlight. In step 1 i got
I just change highscores.xml but suddenly I've got some strange error when i try
I'm just starting to learn web dev, but got myself a bit confused when
I am just starting with f# so the question may seem easy for some
Just starting out in python on a pendrive 12.04 Kubuntu environment. I had to
Just starting with FactoryGirl. I have a Model named Subscription . It has a

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.