Three Classes Share Data???

I

Immortal Nephi

First class is the base class. It has two data: m_Base1 and m_Base2.
Second class and third class are derived classes and they are derived
from first class. m_Base1 and m_Base2 are inherited into two derived
classes.

Second class has its own m_Base1 and m_Base2 and third class does the
same. I am curious. How can second class and third class share the
same m_Base1 and m_Base2?

You define second class first and enter data into m_Base1 and
m_Base2. Then, you define third class and how can you modify a
pointer to second class' m_Base1 and m_Base2.

For example:

class Base
{
Base() {}
~Base() {}
int m_Base1;
int m_Base2;
};

class Derive1 : public Base
{
Derive1() {}
~Derive1() {}
void Set()
{
m_Base1 = 20;
m_Base2 = 30;
}
};

class Derive2 : public Base
{
Derive2() {}
~Derive2() {}
void Print()
{
// How can you do this below?
cout << "Derive1::m_Base1 -> " << Derive1::m_Base1 << endl;
cout << "Derive1::m_Base2 -> " << Derive1::m_Base2 << endl;
}
};

int main(void)
{
Derive1 d1;
Derive2 d2;
d1.Set();
d2.Print();

return 0;
}

Nephi
 
S

Salt_Peter

First class is the base class. It has two data: m_Base1 and m_Base2.
Second class and third class are derived classes and they are derived
from first class. m_Base1 and m_Base2 are inherited into two derived
classes.

Second class has its own m_Base1 and m_Base2 and third class does the
same. I am curious. How can second class and third class share the
same m_Base1 and m_Base2?

You have a single instance of a given class.

class Base { ... };
class Derived : Base { ... };
class DerivedAgain : Derived { ... };

DerivedAgain da;
Derived& d = da;

d is a reference to the Derived portion of instance da.
Assuming that both Derived and DerivedAgain have access to Base's
members, whether you used da or d to access those is irrelevent, both
are accesing the same object.
You define second class first and enter data into m_Base1 and
m_Base2. Then, you define third class and how can you modify a
pointer to second class' m_Base1 and m_Base2.

For example:

class Base
{
Base() {}
~Base() {}
int m_Base1;
int m_Base2;

};

Your ctors above are private, try...

class Base
{
int m_Base1;
int m_Base2;
publc:
Base() : m_Base1(0), m_Base2(0) { }
Base(int b1, int b2) : m_Base1(b1)
m_Base2(b2) { }
};
class Derive1 : public Base
{
Derive1() {}

public:
Derive1() : Base(20,30) { }
~Derive1() {}
void Set()
{
m_Base1 = 20;
m_Base2 = 30;
}

Set() is not needed, and neither would it be alowed to set Base's
private parts.

void Print()
{
std::cout << m_Base1 << std::endl;
...
}
};

class Derive2 : public Base
{
Derive2() {}
~Derive2() {}
void Print()
{
// How can you do this below?
cout << "Derive1::m_Base1 -> " << Derive1::m_Base1 << endl;
cout << "Derive1::m_Base2 -> " << Derive1::m_Base2 << endl;
}

You could pass a reference to a Derived1 instance however i don't see
any logical reason.
Basicly, If you want X to do something, you don't ask Y to do it for
you.

void Print(const Derived1& r_d1)
{
r_dl.Print();
}

Which brings us back to the idea that what you really want is one
object, if that isn't your case, you have a design problem. Does it
really make sense to instantiate some type Derived1 and then
instantiate another derivative of Base just to print the former's
contents? I don't think so.
 
I

Immortal Nephi

First you need to understand when you are talking about objects and when
you are talking about classes.

Every object of type Base from the code you wrote, has its own copy of
m_Base1 and m_Base2. None of the objects (even objects of types that are
derived from Base) share its member-variables with any other object.

If you want all *objects* to share the same m_Base1 and m_Base2, then
make them static:

class Base {
protected:
   static int m_Base1;
   static int m_Base2;

};

class Derive1 : public Base {
public:
   void set() {
      m_Base1 = 20;
      m_Base2 = 30;
   }

};

class Derive2 : public Base {
public:
   void print() {
      cout << "m_Base1 == " << m_Base1 << '\n';
      cout << "m_Base2 == " << m_Base2 << '\n';
   }

};

int Base::m_Base1;
int Base::m_Base2;

int main() {
   Derive1 d1;
   Derive2 d2;
   d1.set();
   d2.print();



}









- Show quoted text -

Okay. I do understand that all objects share variables if static
keyword is used. static variables are like global variables and more
than one class can access global variables. I do not want what you
are tring to say.

I want two objects to share variables. For example, you define two
classes A1 and A2. Class A1 and class A2 share its own variables.
Then, you define another two objects -- class B1 and class B2. Class
B1 andclass B2 have its own copy of shared variables.

Class A1 modifies class A2's variable. Then, class A2 is in turn to
modify class A1's variable.

Let me show you an example.

class Base1;
class Base2;

class Base1
{
public:
Base1() {}
~Base1() {}

void Set1(int a, int b)
{
m_Base1 = a;
base2_ptr->m_Base2 = b;
// error C2027: use of undefined type 'Base2'
}

void Print1()
{
cout << "Base1: " << m_Base1 << endl;
cout << "Base2: " << base2_ptr->m_Base2 << endl;
// error C2027: use of undefined type 'Base2'
}

Base2 *base2_ptr;

private:
int m_Base1;
friend class Base2;
};

class Base2
{
public:
Base2() {}
~Base2() {}

void Set2(int a, int b)
{
base1_ptr->m_Base1 = b;
m_Base2 = a;
}

void Print2()
{
cout << "Base1: " << base1_ptr->m_Base1 << endl;
cout << "Base2: " << m_Base2 << endl;
}

Base1 *base1_ptr;

private:
int m_Base2;
friend class Base1;
};

int main(void)
{
// First two objects
Base1 base1;
Base2 base2;

base1.base2_ptr = &base2;
base2.base1_ptr = &base1;

base1.Set1(10,20);
base1.Print1();

base2.Set2(30,40);
base2.Print2();

base1.Print1();

// Second two objects
Base1 base3;
Base2 base4;

base3.base2_ptr = &base4;
base4.base1_ptr = &base3;

base3.Set1(10,20);
base3.Print1();

base4.Set2(30,40);
base4.Print2();

base3.Print1();

return 0;
}

Why did C++ Compiler gives an error -- error C2027: use of undefined
type 'Base2'? I aready declared class Base2 in the above of class
Base1 definition. Do you have a way to fix it?

Nephi
 
I

Immortal Nephi

Immortal Nephi said:
I want two objects to share variables.  For example, you define two
classes A1 and A2.  Class A1 and class A2 share its own variables.
Then, you define another two objects -- class B1 and class B2.  Class
B1 andclass B2 have its own copy of shared variables.
Class A1 modifies class A2's variable.  Then, class A2 is in turn to
modify class A1's variable.

There you go with the confusion between class and object again. If you
want two objects to share one object (variable), then try to present an
example that also talks about objects (rather than classes.) If you want
two objects to share a variable then you use pointers.

In the below program, there are three objects. Two of type A1, and one
of type int. The two former objects share the one latter object.

class A1 {
   int* var;
public:
   A1():var(0) { }

   void set( int& v ) { var = &v; }
   void modify( int v ) {
      *var = v;
   }
   int value() const { return *var; }
   void print() const {
      cout << *var << '\n';
   }

};

int main() {
   int shared_var = 0;
   A1 obj1;
   A1 obj2;

   obj1.set( shared_var );
   obj2.set( shared_var );

   assert( obj1.value() == obj2.value() );
   obj1.print();
   obj2.print();

   obj1.modify( 12 );

   assert( obj1.value() == obj2.value() );
   obj1.print();
   obj2.print();

}  

Note: the above is generally considered poor design. You should only be
able to change an object's state by calling a public member-function on
that object, but here main can change the state of obj2 by calling a
variable on obj1.


Let me show you an example.

[example snipped]
   Why did C++ Compiler gives an error -- error C2027: use of undefined
type 'Base2'?  I aready declared class Base2 in the above of class
Base1 definition.  Do you have a way to fix it?

You *declared* Base2 before Base1, but you did not *define* it. You
cannot use it until you define it.

Here is your example with the compiler error fixed.

class Base2;

class Base1 {
public:
   void Set1(int a, int b); // can't use Base2 here,
                            // so you can't define these functions here
   void Print1();

   Base2 *base2_ptr;

private:
   int m_Base1;
   friend class Base2;

};

class Base2 {
public:
   void Set2(int a, int b) {
      base1_ptr->m_Base1 = b;
      m_Base2 = a;
   }

   void Print2() {
      cout << "Base1: " << base1_ptr->m_Base1 << endl;
      cout << "Base2: " << m_Base2 << endl;
   }

   Base1 *base1_ptr;

private:
   int m_Base2;
   friend class Base1;

};

// now that Base2 is defined, you can define these functions
// that use Base2
void Base1::Set1(int a, int b) {
   m_Base1 = a;
   base2_ptr->m_Base2 = b;

}

void Base1::print1() {
   cout << "Base1: " << m_Base1 << endl;
   cout << "Base2: " << base2_ptr->m_Base2 << endl;

}

int main() {
   Base1 base1;
   Base2 base2;

   base1.base2_ptr = &base2;
   base2.base1_ptr = &base1;

   base1.Set1(10,20);
   base1.Print1();

   base2.Set2(30,40);
   base2.Print2();

   base1.Print1();

// Second two objects
   Base1 base3;
   Base2 base4;

   base3.base2_ptr = &base4;
   base4.base1_ptr = &base3;

   base3.Set1(10,20);
   base3.Print1();

   base4.Set2(30,40);
   base4.Print2();

   base3.Print1();



}- Hide quoted text -

- Show quoted text -

Thank you for explaining. You use a sentence "If you want two objects
to share a variable then you use pointers". It is an example of my
code to show class Base1 and class Base2 what your sentence meant.
Correct?

Is my code a good design? Two objects can become one when they modify
shared variable through a pointer. If Base class is too large over
200,000 lines, I may have difficulties to focus smaller problem than
larger problem. One object is divided into two objects to reduce the
size of class.

Concentrate to solve smaller problems on both Base1 class and Base2
class. The main function can process two objects to access variables
through a pointer. Is a good design?

Nephi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top