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?

## 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.