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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:46:13+00:00 2026-05-27T01:46:13+00:00

I have to use an array of pointers to Objects and I must also

  • 0

I have to use an array of pointers to Objects and I must also pass it as parameter to methods. However the way to do this eludes me. Here is the method I use for the initialization of the elements of the array. When I dereference them in main, their data are not correct (they contain memory addresses). What is the correct way? Might it be false the way I dereference them?

void createSquares(Square* squareArray[]){

    PropertySquare PropertySquare1(1,"purple",120);
    PropertySquare PropertySquare2(2,"purple",170);

    squareArray[1] = &PropertySquare1;
    squareArray[2] = &PropertySquare2;
.
.
.
}

In main:

Square *allSquares[22] ;
createSquares(allSquares);

cout<<"ID is: "<<allSquares[1]->getID()<<endl;
cin.get();

As I said the ID is finally a memory address.


Update based on answers:

I have tried this and it does not work as well.It is imperative for me to use polymorphism.

vector<Square*> allSquares;
createSquares(allSquares);

void createSquares(vector<Square*> &squares){

PropertySquare PropertySquare1(1,"purple",120);
PropertySquare PropertySquare2(2,"purple",170);

squares.push_back(&PropertySquare1);
squares.push_back(&PropertySquare2);

}

in main:

for (vector<Square*>::iterator it=allSquares.begin(); it!=allSquares.end();it++){
   it->
}

It does not allow me to use the virtual functions of Square since it is abstract.
Any suggestion?

  • 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-27T01:46:13+00:00Added an answer on May 27, 2026 at 1:46 am

    Everything you’re doing is Not Good. It’s tricky to figure out where to begin, so let me start at the end and present The Right Way:

    typedef std::unique_ptr<Square> square_ptr;
    
    void createSquares(std::vector<square_ptr> & v)
    {
      v.emplace_back(new PropertySquare1(1,"purple",120));
      v.emplace_back(new PropertySquare1(2,"green",370));
      // ...
    }
    
    int main()
    {
      std::vector<square_ptr> allSquares;
      createSquares(allSquares);
    
      for (const auto & p : allSquares)
        std::cout << "ID is: " << p->getID() << std::endl;
    }
    

    Now to break down your problems:

    First off, you are storing the pointers of local variables. Those local variables die at the end of the function scope, and the pointers become dangling. Dereferencing is a program error.

    Second, to fix this, you should create dynamic objects: squareArray[1] = new PropertySquare1(1,"purple",120); However, that is problematic, too. Someone will have to clean up those objects! You could iterate over the array and call delete on each element.

    Third, 22 is a “magic number” (because it’s neither 0 nor 1). This should not be hard-coded. If the number really is a compile-time constant, name it somewhere.

    Fourth, either way, don’t use raw arrays. Either use a std::array if the size is known at compile-time, or a std::vector if the size is determined at runtime.

    Fifth, putting it all together, a dynamic container of smart pointers takes care of all your worries. That’s the one presented in my code. The alternative, a static array of smart pointers, wouldn’t use an initialization function at all, but rather it’d be initialized right on the spot:

    const std::size_t nfuncs = 22;
    std::array<square_ptr, nfuncs> allSquares {
      new PropertySquare1(1,"purple",120),
      new PropertySquare1(2,"green",370),
      // ...
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of different type objects and I use a BinaryWriter to
I have an array of strings that I want to use for button titles
I have an SHA-1 byte array that I would like to use in a
I have set an array in my config file that I use global in
I have an array of strings plus one additional string. I want to use
I have the following array: a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] I use it for some visual
I have difficulties to use complex numbers in cuda,pycuda. I have this in C:
Now here's a fun problem. I have an object array as the following: objRequests
I have base class (virtual), with some children, and an array with pointers on
I have created an array of pointers. I have used some of them as

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.