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

Discussion in 'C++' started by Allerdyce.John@gmail.com, Feb 27, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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;
    }
    };
     
    Gianni Mariani, Feb 27, 2006
    #2
    1. Advertising

  3. Markus Moll Guest

    Hi

    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
     
    Markus Moll, Feb 27, 2006
    #3
  4. Guest

    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, 2006
    #4
  5. Tomás Guest

    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
     
    Tomás, Feb 27, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. weafon
    Replies:
    1
    Views:
    339
    Diez B. Roggisch
    Jul 14, 2009
  2. Isti
    Replies:
    2
    Views:
    760
  3. Hoschi-Ingo

    Reference to input item as function parameter

    Hoschi-Ingo, Dec 3, 2003, in forum: Javascript
    Replies:
    6
    Views:
    128
    Hoschi-Ingo
    Dec 3, 2003
  4. AzamSharp
    Replies:
    2
    Views:
    200
  5. hisan
    Replies:
    1
    Views:
    1,407
    Dan Stromberg
    Jun 25, 2012
Loading...

Share This Page