473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Three Classes Share Data???

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
Aug 24 '08 #1
6 2152
On Aug 23, 11:50 pm, Immortal Nephi <Immortal_Ne...@satx.rr.com>
wrote:
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.
>
};

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

return 0;

}

Nephi
Aug 24 '08 #2
Immortal Nephi <Im************@satx.rr.comwrote:
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?
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();
}
>
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
Aug 24 '08 #3
On Aug 24, 8:39*am, "Daniel T." <danie...@earthlink.netwrote:
Immortal Nephi <Immortal_Ne...@satx.rr.comwrote:
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?

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();

}
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- Hide quoted text -

- 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
Aug 24 '08 #4
Immortal Nephi <Im************@satx.rr.comwrote:
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();
}
Aug 24 '08 #5
On Aug 24, 11:47*am, "Daniel T." <danie...@earthlink.netwrote:
Immortal Nephi <Immortal_Ne...@satx.rr.comwrote:
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 -
>>>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.
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
Aug 24 '08 #6
Immortal Nephi <Im************@satx.rr.comwrote:
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?
In a sense yes. But you have class Base1 and Base2 both contain an int,
and the rest of the code is an elaborate attempt to keep those two ints
synchronized.

When you have two objects who's values are dependent on each-other, you
should make a class who's object ensures the two objects in question
stay synchronized.
Is my code a good design?
No.
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.
The thing is, you haven't made it so you can focus on a smaller problem.
The two classes are so tied up in each-other (note the friend
declarations and how they constantly modify each-other to maintain
shared state,) that you are still stuck with one big problem instead of
two smaller ones.

Your base class is over 200,00 lines... The line count alone doesn't
mean bad design, unless there is a lot of repetition. How many
member-variables does it have?
Aug 24 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: NOSPM | last post by:
Don't know if this is the place to post this, but I will give it a go. :) I have a linklist class, and I have around 20 other classes that need to access the same info (the data) that is...
3
by: Bryan Parkoff | last post by:
I have C++ Primer Third Edition -- Author Stanley B. Lippman and Josee Lajoie. I have been studying it for couple months however it does not provide a valuable information which it is about...
6
by: cksj | last post by:
I'm working on a VB.Net DLL project. It has 6 classes. One of the classes has the properties for the database path, database name and server name. The purpose of this class is so that the DLL can...
4
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a...
4
by: Quentin Huo | last post by:
Hi: I have two web sites in my IIS. I want them to share or use one set of classes that I created, because they have same functions that the classses provide. And maybe in the future I will...
6
by: paul perrin | last post by:
I have a class I want to implement in VB.Net - but can't get the behaviour I want. There are two issues - one how should I be getting the behaviour I want, and second is there a better way...
1
by: David Belohrad | last post by:
Dear All, could someone give a hint? I'd like to share the resources as follows: A shared class which works as counter of number of shared resources: class Shared { public: Q_PCBShared()...
18
by: Donn Ingle | last post by:
Hi, I'm getting myself really confused. I have three classes. Two of them need to reference the third, like so: Canvas ---Stack <--- Thing I am doing this right now: s = Stack()
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.