Solved

Common variables between objects

Posted on 2001-06-19
18
215 Views
Last Modified: 2010-04-02

I have a number of objects that are of different classes(derived from the same base class), but certain variables from the different objects are supposed to be common. For example...

class bob: public person
{
    int parents;
    int job;
};

class tom: public person
{
    int parents;
    long children;
};

I need it so if I set bob.parents = 2 then tom.parents also = 2.

Would the best way to do this is set bob and tom as friends, and everytime the parents value changes for one, manually change it for the other? Or is there a way to link these two members so they will alway be the same value?

Thanks
0
Comment
Question by:willmrk
  • 7
  • 7
  • 3
  • +1
18 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 6208316
The easy way to do this would be to declare parents as a class variable of person.

class person
{  public:
      static int parents;
}

This makes parents a class variable. It will be the same storage for any instance of person or any class derived from it.
0
 

Author Comment

by:willmrk
ID: 6208356
This is not possible because all instances of the class person will NOT contain parents (even though both of my examples did). Imagine....

class person
{
  public:
     char* color;
     char* name;
}

class truck: public person
{
    int doors;
    double engine_size;
}

Where bob, tom, and truck may all have a color and name, truck does not have parents.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6208401
Can't you make a special class for bob and tom that has the static varible.

If you can't do this, please give a better example of what you want to do.

The static variable method would be the right method from what you have described.
0
 

Author Comment

by:willmrk
ID: 6208422
for instance..

maybe there is another object of jim (derived from person)and I dont want jim.parents to equal tom.parents and bob.parents.

tom and bob are brothers so if one of there parents die both of there parent values change. jim is unrelated, so the death of bob and toms parents would not effect jim. I dont want to make a seperate class for every instance of brothers that could be possible. These are external objects being loaded in as dlls and I dont know what the future brother cominations will be. Tomorrow I may have to load in skip, dick, and harry all of whom are brothers and share a different member, such as eye_color, but not parents (because they were adopted).

0
 

Author Comment

by:willmrk
ID: 6208489
From the responses I am getting, I am guessing that the best way would be to...

make bob and tom friends and when

void bob.MotherDies
{
  bob.parents = bob.parents - 1;
  tom.parents = bob.parents;
}

and

void tom.MotherDies
{
  tom.parents = tom.parents - 1;
  bob.parents = tom.parents;
}

I have never used the friends comand before. Can I make bob a friend of tom and tom a friend of bob, or can it only go one way? Or if I make tom a friend of bob, can tom access bob and bob access tom?

Thanks in advance for your assistance.

0
 
LVL 30

Expert Comment

by:Axter
ID: 6208491
Use a pointer.

class bob: public person
{
 bob(int &nParent) : parents(nParent)
 {
 };
   int *parents;
   int job;
};

class tom: public person
{
 tom(int &nParent) : parents(nParent)
 {
 };
   int *parents;
   long children;
};

Now when bob's parents changes, so does tom's parents.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6208504
>>I have never used the friends comand before. Can I make
>>bob a friend of tom and tom a friend of bob

The last code you posted has noting to do with using friend unless bob's and tom's parents member is protected.
0
 

Author Comment

by:willmrk
ID: 6208505
this looks like exactly what I need, but I dont understand it. Could you please explain what is going on, or give me a reference in the MSDN Library that will help me out?

Thanks
0
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 6208507
Given the apparent lack of structure, perhaps it would be best to do this with aggregation, rather than inheritance.

Rather than attempt to allow for every possibility in the base class (parents, eye_color, number_of_wheels; whatever), add a reference to an info class in the derived class.

class bob: public person
{
   CInfo *ptr;
   int job;
};

class tom: public person
{
   CInfo *ptr;
   long children;
};

Then pass it into the constructor for them:

bob::bob(CInfo *if)
{   ptr=if;
    return;
}

And to get or set the value use some accessor:

    int parents=ptr->numberOfParents();
    ptr->setNumberOfParents(3);

Then if you want bob and tom to share the same info, pass it the same instance of the info class. If not, pass it some different one.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:willmrk
ID: 6208511
>>The last code you posted has noting to do with using
>>friend unless bob's and tom's parents member is
>>protected.

yes, Im not being very exact with my examples, but you are correct. All the members of bob and tom would be either private or protected.

0
 
LVL 30

Expert Comment

by:Axter
ID: 6208514
class bob: public person
{
bob(int &nParent) : parents(nParent)
{
};
  int *parents;
  int job;
};

class tom: public person
{
tom(int &nParent) : parents(nParent)
{
};
  int *parents;
  long children;
};

class Mother
{
 Mother(int SomeID) : child1(SomeId), child2(SomeID)
{
};
 tom child1;
 bob child2;
};
0
 
LVL 30

Expert Comment

by:Axter
ID: 6208526
That was a bad example.
Here's a better one.

class bob: public person
{
bob(int &nParent) : parents(nParent)
{
};
 int *parents;
 int job;
};

class tom: public person
{
tom(int &nParent) : parents(nParent)
{
};
 int *parents;
 long children;
};

class Mother
{
Mother(int SomeID)
{
MyID = SomeID;
 child1 =  new tom(MyID);
 child2 =  new bob(MyID):
};
 void ChangeID(int NewID)
{
 MyID = NewID;
}
 int MyID;
tom *child1;
bob *child2;
};
0
 
LVL 30

Expert Comment

by:Axter
ID: 6208538
In the above example, Mother has a member interger variable called MyID.  When Mother creates tom and bob, it passes the MyID.
Because the constructor of tom and bob is the following:
bob(int &nParent)
The "&" means that it gets a reference to the original variable.
That reference is passed to bob's and tom's int *parents member.  So that bob's and tom's parents member is pointing to the same variable.

When you call ChangeID() function, it changes the value of MyID.
Because bob and tom points to this variable, the change is automatically reflected in bob and tom's parent variable.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6208546
bob(int &nParent) : parents(nParent)
{
};

When you put a full colon after a constructor, and you follow it my a member variable, you can initialize the member with the value inside of the "(" + ")" parentisies.

In the cpp file that would look like the following:

bob::bob(int &nParent)
: parents(nParent)
{
};

or

bob::bob(int &nParent) : parents(nParent)
{
};
0
 

Author Comment

by:willmrk
ID: 6208589
i have to think about that one. Thank you very much for the explanation, it was very clear and I understant how its working now, but Im not sure if it will work in my application. When we programed in BASIC, we would have two subroutines, Bob and Tom. Bob and Tom would both share the same COM Block so the variables would always be the same when calling the two different routines. I am trying to do the C++ equivialent of this. Maybe this helps explain exactly what Im trying to do.

Again, thanks for your time and advice.
0
 

Expert Comment

by:janiv
ID: 6209726
class Mother: public person{

}

class Child: public person{

public:

  Child(Mother* mother):_mother(mother){}

private:

  Mother* _mother;
}

chlid*   bob = new Child,
         tom = new Child;

mother*  liz =  new Mother;

This is the way it should be!
For further assitance, contact me !
0
 

Author Comment

by:willmrk
ID: 6210387

I appologize for my ignorance and if anybody feels Im waisting their time, but let me explain again what Im trying to accomplish...

Bob and Tom and Jim are objects of different classes that are defined in separate dlls. These dlls are loaded into the engine program in a random order. All three of the dlls might not always be loaded by the engine when its started (this is configured by a database). But if Bob and Tom do both get loaded, then they should share certain members. And if Bob, Tom, and Jim get loaded, then Bob and Tom should still share certain members and Jim would be uneffected.

Im not sure if the mother creating the instances of bob and tom is going to work, because the engine program creates the instances of the objects that I configure the engine to load (via a database). And when I need to access these objects from the code, there is a function that returns a pointer to a specific object..

CBob* pCBob = (CBob) getPointer("BOB");

Where the bob object was titles BOB in the database.

So far from what I understand, I believe the pointer to an info class is the most feasible. Nobody seemed to pick up on this post, is there a problem with doing it this way?

Thanks again.
0
 
LVL 16

Expert Comment

by:imladris
ID: 6214618
Well, I don't think there is a problem with it obviously, and nobody else seems to have thought of any either. If the suggestion did help, then perhaps it is time to select an answer and grade it.........
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now