Passing a reference to an object from inside the object's member functions?

Discussion in 'C++' started by stefven blonqhern, Jul 5, 2007.

  1. Hello, something of a C++ beginner here with a (possibly) (really) bad
    question:

    I want to pass a reference of an object from one of the object's
    member functions. Is this possible?

    so something like this (i've simplified what I'm doing):

    MyFuntion(TestClass& some_object)
    {
    return some_object.val1 * some_object.val2;
    }

    struct TestClass : public MyClass
    {
    int val1;
    int val2;
    }

    int TestClass::passReference
    {
    int x;
    x = MyFunction(this_object); // where this_object is a reference to
    this TestClass!
    }

    How might I get this_object to refer to the correct object? This
    might seem ridiculous but I'm working within a DSP plugin architecture
    if that helps get me off the hook. Is it possible? If so, is it
    dangerous?

    thanks,

    Stefven Blonqhern
    stefven blonqhern, Jul 5, 2007
    #1
    1. Advertising

  2. stefven blonqhern wrote:
    > Hello, something of a C++ beginner here with a (possibly) (really) bad
    > question:
    >
    > I want to pass a reference of an object from one of the object's
    > member functions. Is this possible?


    Sounds possible.

    > so something like this (i've simplified what I'm doing):
    >
    > MyFuntion(TestClass& some_object)


    No return value type. Did you mean

    ValueType MyFunction(TestClass& some_object)

    ?

    Also, why isn't 'someobject' a reference to *const* TestClass? As in

    ValueType MyFunction(TestClass const & some_object)

    ??

    > {
    > return some_object.val1 * some_object.val2;
    > }
    >
    > struct TestClass : public MyClass
    > {
    > int val1;
    > int val2;
    > }

    ;

    >
    > int TestClass::passReference


    Did you mean to have an argument list succeeding the name of the
    function, as in

    int TestClass::passReference()

    ???

    > {
    > int x;
    > x = MyFunction(this_object); // where this_object is a reference to
    > this TestClass!


    Have you tried derefereincing 'this'? As in

    x = MyFunction(*this);

    ????

    > }
    >
    > How might I get this_object to refer to the correct object? This
    > might seem ridiculous but I'm working within a DSP plugin architecture
    > if that helps get me off the hook. Is it possible? If so, is it
    > dangerous?


    No, it's not dangerous.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 5, 2007
    #2
    1. Advertising

  3. On Jul 5, 11:18 pm, stefven blonqhern <>
    wrote:
    > Hello, something of a C++ beginner here with a (possibly) (really) bad
    > question:
    >
    > I want to pass a reference of an object from one of the object's
    > member functions. Is this possible?
    >
    > so something like this (i've simplified what I'm doing):
    >
    > MyFuntion(TestClass& some_object)
    > {
    > return some_object.val1 * some_object.val2;
    >
    > }


    Two things
    1. MyFunction must have a return type. (I guess you forgot to type
    that)
    2. Since you are not modifying some_object, pass it as a const
    reference
    int MyFunction (const TestClass& some_object)


    > struct TestClass : public MyClass
    > {
    > int val1;
    > int val2;
    >
    > }
    >

    missing semicolon at end of struct definition, you should also
    declare PassReference in the class, since you are defining it below.

    > int TestClass::passReference
    > {
    > int x;
    > x = MyFunction(this_object); // where this_object is a reference to
    > this TestClass!


    x = MyFunction(*this);


    > thanks,
    >
    > Stefven Blonqhern
    Neelesh Bodas, Jul 5, 2007
    #3
  4. Okay many thanks Victor and Neelesh.

    > Two things
    > 1. MyFunction must have a return type. (I guess you forgot to type
    > that)


    Yes i forgot that. My code was simplified just to illustrate my
    overall intentions.

    > 2. Since you are not modifying some_object, pass it as a const
    > reference
    > int MyFunction (const TestClass& some_object)


    Actually I will be modifying some_object.

    > x = MyFunction(*this);


    Right, I didn't imagine it would be that simple for some reason.

    Thanks again, Stefven.
    stefven blonqhern, Jul 5, 2007
    #4
    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. ce
    Replies:
    1
    Views:
    2,982
  2. Steven T. Hatton

    Passing member functions to C functions?

    Steven T. Hatton, Oct 4, 2004, in forum: C++
    Replies:
    7
    Views:
    1,316
    David Hilsee
    Oct 7, 2004
  3. Hicham Mouline
    Replies:
    0
    Views:
    431
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    412
    Michael DOUBEZ
    Apr 24, 2009
  5. A. Farber
    Replies:
    5
    Views:
    171
    A. Farber
    Aug 23, 2008
Loading...

Share This Page