473,385 Members | 1,848 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,385 software developers and data experts.

How to access a data member of class A from a member function of class B?

13
Hi,

I'm a beginner and need help with the following:

I have defined a class A where a private data member is an array of structs;

private:
vector<a_struct> Data;

The values of the structs in the array are set by the constructor (by reading data from a file) when an object of class A is created. The values of 'Data' can be accessed by a 'getData' memberfunction.

The problem is that I want to access 'Data' from a memberfunction of another class B. How can I do that without creating a new object of class A everytime i call the memberfunction of class B?

Thanks for the help...
Oct 12 '06 #1
3 1692
Banfa
9,065 Expert Mod 8TB
Where are you instantiating class A?
Oct 12 '06 #2
Curten
13
As it is now, class A is instantiated in main, something like this:

main
{
ClassA objectA(filename); //Values of datamember 'Data' are set.
a_function();

other operations using objectA...

}

in the definition of a_function(), there is a call to another function "assignCharge". "assignCharge" sets the values of datamembers of
objectB of class B and here I want to use the values of 'Data' to control the values set to objectB.

This is perhaps not the best "software engineering", but I'm supposed to edit a program of approx. 10,000 lines made by another person so this is basically how it has to be done... =)

Again, thanks
Oct 12 '06 #3
Banfa
9,065 Expert Mod 8TB
Well you need objectA to be in scope in the called functions. Ways of doing this are

1. Make objectA global
2. Pass a refernce to objectA down into the functions
3. Make objectA a static member of class B (it there is only ever 1 instance of objectA).
4. Make objectA a singleton class, if there is only every 1 instance of objectA. objectA is a static member of class A.

The over-riding theme of all these is that there is only a single instance of objectA at any 1 time.
Oct 12 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Marcin Kowalewski | last post by:
Hi I've got a stupid problem with code below : using System; using System.Drawing; .... public class CsrcImage :Image { public CsrcImage() { //
1
by: Edward Forgacs | last post by:
Is there any way to get a reference to the base class of a base class from an inherited class? I need to call an overriden method. Any help would be much appreciated.
5
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
6
by: Ken Foster | last post by:
Is it possible to return a class (not an instance of a class) from a function? For example (actually tried this code, it didn't work, but it's what I want sort of): Class EventMessage Public...
2
by: Rob Long | last post by:
Hi there Is there any way to access private variables directly from within a priviliged function? I have a situation where the priviliged function's execution context contains variables of the...
1
by: markww | last post by:
Hi, I have this class: class CBase { int m_nData; OnStart(); OnRightMouseClick(); }
6
by: Bruce | last post by:
I need to fire an event from an unmanaged class. If I declare a pointer to the managed class within the unmanaged class I get a compile error " "cannot declare a managed 'm_pGps' in an unmanaged...
5
by: kkirtac | last post by:
Hi, i have a class "myClass", and i want to return two instances of the same class from a function. I define an array as follows : "myClass sample ;" and i initialize two instances: "myClass...
2
by: john spencer | last post by:
How to return vector of class from a function. Is this function and her calling correct: Code removed per posting guidelines
6
by: historysav | last post by:
Here my code, when I write "print $query{2}{ctf};", I got nothing but When I write inside of function, I got value.... How can I access it from outouf function? How can I send my %query hash to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.