How would you change the same member of many objects of the same class (or structure if possible)?

struct point
{
    float x, y, z;
}

point a, b, c;

a.x += 20;  //
b.x += 20;  // now how would i do this in one statement?
c.x += 20;  //

I don't see how inheritance or polymorphism would apply, and as far as I know they don't work with structures anyway, and I really have no need of classes. So how would I change all point::x members universally regardless of how many or what objects use it?

Dani AI

Generated

Two different goals are being mixed in the thread: either make a single shared x for every object (use a static member, as suggested), or keep one x per object but update many objects at once (what was pointing toward). If each instance must keep its own x value, do not use static. Instead group the instances so you can iterate them.

A few practical options (pick what matches your code layout):

  • Preferable when you control creation: store objects in a container and update with a loop or algorithm.

    std::vector<Point> pts = /* fill */;
    for (auto &p : pts) p.x += 20;

    (See range-based for details: https://en.cppreference.com/w/cpp/language/range-for)

  • If objects are separately named and you cannot change construction, collect pointers/references and iterate.

    Point* list[] = { &a, &b, &c };
    for (auto p : list) p->x += 20;
  • To write generic update code that picks a member at runtime, use a pointer-to-member.

    float Point::* px = &Point::x;
    for (auto &p : pts) p.*px += 20;

    (Pointer-to-member reference: )

If you need the “update every instance no matter where it was created” behavior, implement a small registration pattern inside the type: have a static container of this pointers, push in the constructor and remove in the destructor, then provide a static method to apply changes to all registered instances. Be careful to make removal safe (store an iterator or use a linked list) and guard the registry with a mutex if instances can be created/destroyed concurrently.

Recommendation: for clarity and performance, keep objects in a contiguous container (std::vector) when you intend to update many of them at once. If that is not possible, use a pointer-array or a class-level registry.

Recommended Answers

All 6 Replies

How about declaring x as a [search=1911904612]static member[/search] of class point?

Well I need each x member to have different instances, like this:

struct point
{
    float x, y, z;
}

point a, b, c;

a.x = 10;
b.x = -50;
c.x = 2000.3258;

a.x += 20;  //
b.x += 20;  // now how would i do this in one statement?
c.x += 20;  //

and yet be able to modify them all, even if there were hundreds of instances of point.

Well I need each x member to have different instances, like this:

struct point
{
    float x, y, z;
}
 
point a, b, c;
 
a.x = 10;
b.x = -50;
c.x = 2000.3258;
 
a.x += 20;  //
b.x += 20;  // now how would i do this in one statement?
c.x += 20;  //

and yet be able to modify them all, even if there were hundreds of instances of point.

How about creating an array of points, then using a loop to traverse each element of the array?

Okay, I'll use a multidimesional array to store the 'objects'. I just thought it would be helpful if I could name them different things and use the default copy constructor/copy assignment constructor, like b = a; or point top (c); . Don't you think such a member-modifying feature should be added to C++? It would be awesome.
:)

Don't you think such a member-modifying feature should be added to C++?

No.

Care to be a little more specific?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.