473,799 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(TestC lass& some_object)
{
return some_object.val 1 * some_object.val 2;
}

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

int TestClass::Pass Reference
{
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 1286
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(TestC lass& some_object)
No return value type. Did you mean

ValueType MyFunction(Test Class& some_object)

?

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

ValueType MyFunction(Test Class const & some_object)

??
{
return some_object.val 1 * some_object.val 2;
}

struct TestClass : public MyClass
{
int val1;
int val2;
}
;
>
int TestClass::Pass Reference
Did you mean to have an argument list succeeding the name of the
function, as in

int TestClass::Pass Reference()

???
{
int x;
x = MyFunction(this _object); // where this_object is a reference to
this TestClass!
Have you tried derefereincing 'this'? As in

x = MyFunction(*thi s);

????
}

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...@y ahoo.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(TestC lass& some_object)
{
return some_object.val 1 * some_object.val 2;

}
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::Pass Reference
{
int x;
x = MyFunction(this _object); // where this_object is a reference to
this TestClass!
x = MyFunction(*thi s);

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(*thi s);
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
2002
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> bool1=True lst1= def func1(arg1): arg1.append(4)
32
5024
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 not a function" ---------------------------------------------------- I have this in a .js file and in the head section.
7
3307
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
8
4429
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 looks like this header file
0
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10269
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10032
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.