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 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

Ask A Question

Stats

  • Questions 61k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer No, there is no easy way to determine if a… May 11, 2026 at 9:57 am
  • added an answer Not sure about your particular setup, but QTP has an… May 11, 2026 at 9:57 am
  • added an answer Unfortunately the C# projects do not support anything like this… May 11, 2026 at 9:57 am

Related Questions

I am just starting with C++ and got some problems in understanding how the
I am just starting with coding some JSP, which I find isn't all that
I am just starting out with DI & unit testing and have hit a
I am just starting out with Silverlight (2 RC0) and can’t seem to get
I am just starting to fiddle with Excel via C# to be able to
I am just starting to use Subversion with Tortoise SVN client for one of
I'm just starting with CodeIgniter, and I am not sure where things such as
I've been working with python for a while now and am just starting to
I am just starting to learn javascript, so I don't have the skills to
I am just starting to learn F#. In several F# coding examples I see

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.