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

The Archive Base Latest Questions

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

I have to admit my C++ is a bit rusty. In a project I

  • 0

I have to admit my C++ is a bit rusty. In a project I try to create a vector of classes and use them. There is a problem because I want to identify each entry of the vector with a unique pointer to it for fast access but it does not work. Here is a minimal example of my problem:

#include <iostream>
#include <vector>

class Foo{
public:
Foo() { ptr = this; }
~Foo() {}
Foo * ptr;
};

int main()
{
std::vector<Foo> vec;
for(unsigned int i = 0; i < 2; ++i)
  vec.push_back(Foo());
for(unsigned int i = 0; i < vec.size(); ++i)
 std::cout << "Object Self-Pointer: " << std::hex << reinterpret_cast<unsigned int>(vec[i].ptr) << std::endl; 
}

Actual output:

Object Self-Pointer: bfbebc18
Object Self-Pointer: bfbebc18

Intended output:

Object Self-Pointer: bfbebc18
Object Self-Pointer: bfbebc1c

(some pointers to the actual objects).

I hope you can help me with this issue, thank you in advance.

  • 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-31T01:38:15+00:00Added an answer on May 31, 2026 at 1:38 am

    When you do this:

    vec.push_back(Foo());
    

    A new temporary Foo object is created, but each time it is created in the same address (because it is created on stack and the stack pointer is the same). So the this pointer, and therefore the ptr member of each of these instances is the same.

    When you add each instance to the vector, the default copy constructor is called, so the ptr member is copied, and therefore all instances in the vector have the same value.

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

Sidebar

Related Questions

I admit that I have almost none experience of unittesting. I did a try
I must admit I have researched a bit but keep coming into a block
I have a winforms app with multiple GUI threads. I want them to be
I have to admit that I'm new to Java and Android. db4o seems to
First of all I have to admit that my programming skills are pretty limited
I've seen this and I have to admit I'm pretty impressed: (source: blogspot.com )
This is my first post and I have to admit, I am terrible at
First, I have to admit I screwed up a little with CVS. I had
After looking around (for not terribly long I have to admit) I wonder if
I did my programming before integrating into a design and I have to admit

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.