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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:29:37+00:00 2026-06-13T07:29:37+00:00

I have a pointer to an object which i will be using in one

  • 0

I have a pointer to an object which i will be using in one method. but i need to use the same pointer again in another method how can i achieve this without declaring as global object. This is part of my dynamic biding achievement
Shape is the parent class and the Rectangle is the child class.

int main(){
switch (choice){
case 1:
 create();
 break;
case 2:
 process();
 break;
 }
}

create(){
Shape *shape[3];
shape[0]=&objRectangle;
}

process(){
for(int i=0;i<3;i++){
shape->print; // I want to acchieve like this.
}

Now i cant do this cause, the shape object is gone once it exits the create process.
Please assist.

  • 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-13T07:29:38+00:00Added an answer on June 13, 2026 at 7:29 am

    I would suggest, as others do, to let the library manage the memory for you.

    In order to be able to use dynamic binding and std::vector you should start allocating (in your main) your vector as

        std::vector<Shape*> shape(3);
    

    Doing so you can access your dynamically bound vector entries as

        shape[0]->process();
    

    The bad thing is that you still have to manage the memory pointed by vector entries (they are just C pointers, in fact). Hence, why don’t you consider doing

        std::vector< std::tr1::shared_ptr< Shape > > shape(3);
    

    ?

    Doing this way, the smart pointer std::tr1::shared_ptr will free the memory for you when the pointed Shape object goes out of scope.

    Moreover, in this setting, you should allocate Shape-type objects as

         shape[0] = std::tr1::shared_ptr< Shape >(new Rectangle);
    

    to properly create the smart pointer you need.

    Finally, the vector shape should be passed by reference (or const reference) to functions using it.

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

Sidebar

Related Questions

I have a Snake class, which has a pointer to a Controller class object.
I've search everywhere for a pointer to this, but can't find one. Basically, I
I have the following unordered_map, that maps a pointer to an object of type
I have a native object (C++) that has a gcroot pointer to a managed
Hi I have a question about this pointer, when an object is constructed, when
Using Hashtable I want to have multiple objects mapped to the same key. E.g.
I am using 2 text boxes which will input int digits to max length
I have an object myObject in my UITableViewController set as one of its properties
I have a problem where I want to clone of a object pointer when
I have the following code which works as expected: #include <iostream> using namespace std;

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.