Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How can I keep reference to an input parameter of a function?

Reply
Thread Tools

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

 
 
Allerdyce.John@gmail.com
Guest
Posts: n/a
 
      02-27-2006

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.

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      02-27-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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;
}
};




 
Reply With Quote
 
 
 
 
Markus Moll
Guest
Posts: n/a
 
      02-27-2006
Hi

(E-Mail Removed) 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

 
Reply With Quote
 
Michiel.Salters@tomtom.com
Guest
Posts: n/a
 
      02-27-2006

(E-Mail Removed) 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

 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      02-27-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How keep python socket alive for ever by setting Keep alive flag. hisan Python 1 06-25-2012 05:30 PM
decltype as a template parameter when containing reference to anothertemplate parameter. Isti C++ 2 04-19-2010 10:01 PM
Passing HttpContext reference as an input parameter josephpage@gmail.com ASP .Net 1 04-13-2005 11:38 PM
Reference to input item as function parameter Hoschi-Ingo Javascript 6 12-03-2003 01:41 PM



Advertisments