Hi friends i have one question about OOP.
I have

class A 
{
/function body here
};

class B:public A
{
/function body here
};

class C:public A
{
/function body here
};

int main()
{
B b[20];
C c[40];
}

Each object of the derived classes(B and C)has x,y and z coordiantes. My problem is here how can i update new x,y and z values for all the objects simultaneously. Every time i calculate x,y and z values for all the objects and i need ti update x,y and z values for all the objects simultaneously...not one by one...

Recommended Answers

All 2 Replies

Hmm, tough cookies. By the time you've instantiated objects, each object has a separate copy of the variables, so you have to update each object separately.

That said, it's possible to update all of the variables at once, but only if they're inherited as static members to class A:

#include <iostream>

class A {
public:
  static int x;
};

int A::x;

class B: public A {};
class C: public A {};

int main()
{
  B b;
  C c;

  b.x = 12345;

  std::cout<< b.x <<'\n';
  std::cout<< c.x <<'\n';
}

However, this also means that every object will always have the same set of values. They can't be independent and simultaneously updated at the same time.

> i need ti update x,y and z values for all the objects simultaneously...not one by one...
if you need to share members for some (not all) objects (perhaps for some period of time), consider using std::tr1::shared_ptr or boost::shared_ptr to hold the members. http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/shared_ptr.htm shared pointers will also allow sharing of members across objects belonging to different classes.

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.