473,396 Members | 1,990 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,396 software developers and data experts.

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

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

Jul 5 '07 #1
3 1264
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
Jul 5 '07 #2
On Jul 5, 11:18 pm, stefven blonqhern <robo_cree...@yahoo.co.uk>
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

Jul 5 '07 #3
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.

Jul 5 '07 #4

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

Similar topics

20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
32
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...

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.