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

  • Thread starter stefven blonqhern
  • Start date
S

stefven blonqhern

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
 
V

Victor Bazarov

stefven said:
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
 
N

Neelesh Bodas

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);
 
S

stefven blonqhern

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top