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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:07:54+00:00 2026-06-14T20:07:54+00:00

Consider a Drag object: class Drag { public: Drag(ofVec3f _pos); ofVec3f pos; } Position

  • 0

Consider a Drag object:

class Drag {
public:
    Drag(ofVec3f _pos);
    ofVec3f pos;
}

Position is stored when a new instance is created:

Drag::Drag(ofVec3f _pos) {
    pos = _pos;
}

Position is updated when mouse moves:

void Drag::mouseMoved() {
    pos.x = ofGetMouseX();
    pos.y = ofGetMouseY();
}

In the application’s main class (testApp in openframeworks):

class testApp : public ofBaseApp {
    public:
        vector<Drag> drags;
        vector<ofVec3f *> points;
}

Create a Drag when the mouse is pressed and store its position in a vector called points:

void testApp::mousePressed(int x, int y, int button) {
    Drag drag = Drag(ofVec3f(ofGetMouseX(), ofGetMouseY(), 0));
    drags.push_back(drag);
    points.push_back(&drag.pos);
}

Now when the drag is moved, I can see its position update, but points[0] doesn’t change:

void testApp::update(){
    if (!drags.size()) return;
    cout << drags[0].pos.x << ", " << drags[0].pos.y << endl;
    cout << &points[0]->x << ", " << &points[0]->y << endl;
}

If the type of points is vector<ofVec3f> it seems that points[0] is a copy of the initial drags[0].pos. If it is vector<ofVec3f *> then it seems to store an address on memory that is equal to &drag.

How can I make points[0] point to drags[0].pos and get its x,y values updated when drags[0].pos.x and drags[0].pos.y are updated?

How can I make points[0] be a reference to drags[0].pos?


Edit: Thanks to yonilevy for pointing me in the right direction. Here’s the working example, updated using std::list:

// testApp.h
list<Drag> drags;
vector<ofVec3f *> points;

// testApp::mousePressed
drags.push_back(Drag(ofVec3f(ofGetMouseX(), ofGetMouseY(), 0)));
points.push_back(&drags.back().pos);
  • 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-14T20:07:55+00:00Added an answer on June 14, 2026 at 8:07 pm

    Your mistake is in thinking storing &drag.pos will help you keep track of drag‘s position, while in fact you are storing an address that will become meaningless by the end of that scope. Since drag is a local variable, living on the stack, it will be gone by the end of the scope in which it was created. Even storing the address of the copy of drag that’s being created by the vector when you push_back won’t help in this case, since it will also become invalid once the vector grows in physical size and has to move all of its stuff to a different place in memory.

    What you could do, is make sure the actual Drag object you push to the vector remains the only one around, by allocating it on the heap and pushing a (smart) pointer to it into the vector. That way, you can keep a pointer to its internal pos in a vector just like you did, and it will be valid as long as the other vector is.

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

Sidebar

Related Questions

Consider: Public MustInherit Class Column Public ReadOnly Property ReturnSomethingUseful() As Object Get 'return something
Consider the following class: public class Score { private static readonly Guid _relationId =
consider a fun.cpp file : class fun { public: void sum(); void dispaly(); };
Consider this simple controller: Porduct product = new Product(){ // Creating a product object;
Consider this simple class: package net.zomis.test; public class Test { public Test() { }
I'm trying to implement a reorder-by-drag-and-drop feature to an ObjectListView. Consider the following class:
consider the below class public class Player { private int id; public int getId()
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following Mechanize form object #<Mechanize::Form {name f1} {method POST} {action f.php} {fields
Consider the following code: public interface A { public A another(); } public interface

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.