473,406 Members | 2,707 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,406 software developers and data experts.

How can I keep reference to an input parameter of a function?


I have a function in a class:
void A::aFunction (B& b) {
// do something
....
}

void A::anotherFunction() {
// need a reference of B again.

}

my question is how can I create an attriubte of A which can hold B
after A::aFunction() is called?
I can't create a reference of B as an attribute of A (since aFunciton
is not called during constructor of A).

Thanks for any idea.

Feb 27 '06 #1
4 1547
Al************@gmail.com wrote:
I have a function in a class:
void A::aFunction (B& b) {
// do something
...
}

void A::anotherFunction() {
// need a reference of B again.

}

my question is how can I create an attriubte of A which can hold B
after A::aFunction() is called?
I can't create a reference of B as an attribute of A (since aFunciton
is not called during constructor of A).

Thanks for any idea.

Using a pointer ... somthing like so ?

struct B;

struct A
{

B * m_b;
A()
: m_b(0)
{}

void aFunction (B& b)
{
m_b = &b;
}

void anotherFunction()
{
assert( m_b );
B & b = * m_b;
}
};


Feb 27 '06 #2
Hi

Al************@gmail.com wrote:
I have a function in a class:
void A::aFunction (B& b) {
// do something
...
}

void A::anotherFunction() {
// need a reference of B again.

}

my question is how can I create an attriubte of A which can hold B
after A::aFunction() is called?
I can't create a reference of B as an attribute of A (since aFunciton
is not called during constructor of A).


There are reference wrappers (boost::reference_wrapper), or you could write
one yourself. It's basically struct wrapper { B& ref; wrapper(B& ref) :
ref(ref) {} };
But I don't like the whole idea of caching some B in A, as it relies on the
user's first calling aFunction. Why not return some object from aFunction
on which you can invoke anotherFunction?

Markus

Feb 27 '06 #3

Al************@gmail.com wrote:
I have a function in a class:
void A::aFunction (B& b) {
// do something
...
}

void A::anotherFunction() {
// need a reference of B again.

}

my question is how can I create an attriubte of A which can hold B
after A::aFunction() is called?


Do you really need it? What if the B object is destroyed after
A::aFunction
returns? What if aFunction is called twice? Which B is needed then?
You might be able to hold a copy of B. If it's designed properly, it
will have
a copy constructor if and only if you can copy it. Of course, that
means that
anotherFunction will work on a copy of b, but at least A can ensure the
lifetime of that copy.

A proper design would probably involve a smart pointer. Either
std::auto_ptr<B>,
boost::shared_ptr<B> or std::tr1::shared_ptr<B> could work.

HTH,
Michiel Salters

Feb 27 '06 #4
posted:

I have a function in a class:
void A::aFunction (B& b) {
// do something
...
}

void A::anotherFunction() {
// need a reference of B again.

}

my question is how can I create an attriubte of A which can hold B
after A::aFunction() is called?
I can't create a reference of B as an attribute of A (since aFunciton
is not called during constructor of A).

Thanks for any idea.


Add the following to A:

class A
{
protected:

B* p_b;
};
Then write the functions as follows:
void A::aFunction (B& b)
{
p_b = &b;
}
Then to use it as a reference in another function:
void A::anotherFunction()
{
B& b = *p_b;

//Now we can use "b" as we please:

b.EatGrass();

FunctionThatTakesB( b );
}

-Tomás

Feb 27 '06 #5

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

Similar topics

6
by: Hoschi-Ingo | last post by:
Hello NG, I want to write a generic function to validate data in input items. For this I want to pass a refernce to the object which calls this function to read out the and modify it if...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
1
by: john woo | last post by:
Hi I'm not good at JS, but want to get more about it. I want to use a JSP (the java code just used to get date, the rest are html and javascript), to display a table. the requirement is the...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
6
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
4
by: kasthurirangan.balaji | last post by:
Hi, Recently i came across a functiion declaration as below void f(char *&a); Nowhere i have come across this style. I understand, passing by reference is always better(to avoid copying),...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
17
by: Nyris | last post by:
I have this code used for the navigation on an HTML page. It's an accordion style dropdown menu. Everything works fine I just need to change it so that the submenuheader class allows it to be a...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.