473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class / Member Class relationship?

Hi Guys,

I have a question. Here goes:

class clsManager {
public:
int x;
int y;

}

class clsContainer {
public:
int a;
int b;
clsManager oManager;

}

So I have these two classes, first class is a member of second class.
Is it possible to to access members of class clsContainer from the
object oManager ? How can it be done ?

Thanks & Best Regards.
Faisal.

Aug 18 '06 #1
6 1459
In article <11************ *********@p79g2 000cwp.googlegr oups.com>,
faisalee <fa******@gmail .comwrote:
>I have a question. Here goes:

class clsManager {
public:
int x;
int y;

}

class clsContainer {
public:
int a;
int b;
clsManager oManager;

}

So I have these two classes, first class is a member of second class.
Is it possible to to access members of class clsContainer from the
object oManager ? How can it be done ?
Not in any portable way. What problem are you trying to solve?
Perhaps there is an other solution barking up another tree.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 18 '06 #2

faisalee wrote:
Hi Guys,

I have a question. Here goes:

class clsManager {
public:
int x;
int y;

}

class clsContainer {
public:
int a;
int b;
clsManager oManager;

}

So I have these two classes, first class is a member of second class.
Is it possible to to access members of class clsContainer from the
object oManager ? How can it be done ?
Add a pointer to a clsContainer to the clsManager class.

class clsManager {
public:
clsManager(clsC ontainer* c) : container(c) {}
int x;
int y;
clsContainer* container;
};

John

Aug 18 '06 #3

faisalee wrote:
Hi Guys,

I have a question. Here goes:

class clsManager {
public:
int x;
int y;

}

class clsContainer {
public:
int a;
int b;
clsManager oManager;

}

So I have these two classes, first class is a member of second class.
Is it possible to to access members of class clsContainer from the
object oManager ? How can it be done ?

Thanks & Best Regards.
I would question why you need to do this. Perhaps you could use the
construct that's described in 24.3 in the faq ( which I quote below
)...

A legitimate, long-term use for private inheritance is when you want to
build a class Fred that uses code in a class Wilma, and the code from
class Wilma needs to invoke member functions from your new class, Fred.
In this case, Fred calls non-virtuals in Wilma, and Wilma calls
(usually pure virtuals) in itself, which are overridden by Fred. This
would be much harder to do with composition.

class Wilma {
protected:
void fredCallsWilma( )
{
std::cout << "Wilma::fredCal lsWilma()\n";
wilmaCallsFred( );
}
virtual void wilmaCallsFred( ) = 0; // A pure virtual function
};

class Fred : private Wilma {
public:
void barney()
{
std::cout << "Fred::barney() \n";
Wilma::fredCall sWilma();
}
protected:
virtual void wilmaCallsFred( )
{
std::cout << "Fred::wilmaCal lsFred()\n";
}
};

Aug 18 '06 #4
Hi Greg,

Thanks for the reply. Actully I am maintaining a project that was
written way back in 1998! Well talking about the main classes, it has
two of them,

DBMgr and DBOps

DBMgr is responsible for opening a connection to the SQL Database and
the DBOps manages the SQL Statements like Insert, Update / Delete etc.

The DBOps is a member class of DBMgr and in the Connection handle is
assigned to the DBOps object through its constructor.

Now the problem is that other classes uses a ref object of the DBOps to
perform DB Operations directly. There are times when the DB Connection
gets broken and DBOps dont know how to connect / reconnect back. Since
only DBMgr knows how to do it.

So I thought if I can get a ref of DBMgr inside DBOps class, I can just
call the connect method which will automatically assign the connection
handle to the required classes.

I hope I was able to explain clearly... I dont see if there is any
other way.

Oh yes I thought of defining the Connect / Disconnect method as static
but then I will have to change all the member variables used inside
them as static too... THIS I dont want to do :-)

Is there any other way to do this? Or usin this cyclic relation is the
only way?

Best Regards,
Faisal.

Greg Comeau wrote:
In article <11************ *********@p79g2 000cwp.googlegr oups.com>,
faisalee <fa******@gmail .comwrote:
I have a question. Here goes:

class clsManager {
public:
int x;
int y;

}

class clsContainer {
public:
int a;
int b;
clsManager oManager;

}

So I have these two classes, first class is a member of second class.
Is it possible to to access members of class clsContainer from the
object oManager ? How can it be done ?

Not in any portable way. What problem are you trying to solve?
Perhaps there is an other solution barking up another tree.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 19 '06 #5
Thanks Jhon,

I will give it a try :-)

Regards,
Faisal.

jo************* @hotmail.com wrote:
faisalee wrote:
Hi Guys,

I have a question. Here goes:

class clsManager {
public:
int x;
int y;

}

class clsContainer {
public:
int a;
int b;
clsManager oManager;

}

So I have these two classes, first class is a member of second class.
Is it possible to to access members of class clsContainer from the
object oManager ? How can it be done ?

Add a pointer to a clsContainer to the clsManager class.

class clsManager {
public:
clsManager(clsC ontainer* c) : container(c) {}
int x;
int y;
clsContainer* container;
};

John
Aug 19 '06 #6
faisalee wrote:
Thanks Jhon,
Please don't top-post. See the newsgroup FAQ under "How to Post".


Brian
Aug 19 '06 #7

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

Similar topics

6
2179
by: Chris Mantoulidis | last post by:
Forgive me if I'm wrong but I think there is something like an extra member scope in classes. for example: class abc { ostream & operator << (ostream &, const abc &); istream & operator >> (istream &, abc &); private:
13
2389
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating and deallocating two sub-classes. CMemory class is responsible to allocate and deallocate memory...
8
2849
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl list class must Ioverride in order for this to work?
0
1168
by: nin234ATIyahoo.com | last post by:
I am wondering what is a good design pattern in terms of C++ class relationship for the problem I am trying to solve. template<class DbKey, class DbVal, class DbTag> class srvBerkDB : public Db //class for reading and writing to Berkeley DB { VecB KeyPosn; DbTag tag; //Only a snippet of the class is shown here.
3
1848
by: Shannon Rotz | last post by:
I'm just learning how to use class modules in Access (2000 or XP). So far, I've created two classes, with the data manipulation for the corresponding tables encapsulated inside each of them Member Membership The problem is that one Member can have many Memberships, and I'm having a problem figuring out how to relate them. Ideally I'd like my calling code to be able to say:
1
986
by: Jodie | last post by:
Hi All, I have a question regarding about inheritance and member of the class. For example I have the class A and class B what is the situation that I need to define class B is base class of class A or class B is member of class A like below: Scenario 1: class A : public B Scenario 2: or class A
8
11441
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with another form so to declare the sub I use visual basic code: Public Shared Function IsPortAvailable(ByVal ComPort As Integer) As Boolean
18
2903
by: sd2004 | last post by:
could someone please show/help me to copy all element from "class dog" to "class new_dog" ? Note: "class new_dog" has new element "age" which should have value "my_age" /////////////////////////////// source code /////////////////////////////// #include<iostream> #include <string> #include<vector> #include<sstream>
8
8932
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.