473,668 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing pointers to methods - all of it this time!

Hi - sorry! My previous message got screwed up, evidently :-$

I have a weird case of a pointer, passed as an argument to a method,
not being assigned to the value it should.

My method is like this:

bool myClass::myMeth od(param a, toBeUpdated* b)
{

if(a == myDataMember) {
b = &someData;
return true;
}
else return false;
}
I call it via a pointer to an object:

myClass* s;
toBeUpdated* updated = 0;

if( s->myDataMember(a ,updated) ) {
cout << updated->something << endl; // SEGFAULTS!
}
else {
cout << "Did not return anything in 'updated'" << endl;
}
Now, the issue is that for some reason, when the method returns true,
the value assigned to b in the method does not make it out - in other
words, the value of 'updated' is not changed by calling the method.
Am I missing a trick, or is this a bug in gcc?

Thanks,

Chris
Dec 4 '07 #1
4 1356
Equus schrieb:
if(a == myDataMember) {
b = &someData;
Use're using call-by-value here! What you mean is

*b = &someData
cout << updated->something << endl; // SEGFAULTS!
Otherwise "updated" will stay NULL - and segfault.

Greetings,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gotteslästerli ch. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
HJP in de.sci.mathemat ik <47************ **********@news .sunrise.ch>
Dec 4 '07 #2
Doh! Thanks Johannes! I will get myself a new pair of spectacles :-)

Chris
Dec 4 '07 #3
Equus wrote:
Hi - sorry! My previous message got screwed up, evidently :-$
Aha...
I have a weird case of a pointer, passed as an argument to a method,
not being assigned to the value it should.

My method is like this:

bool myClass::myMeth od(param a, toBeUpdated* b)
{

if(a == myDataMember) {
b = &someData;
Here you update the local object, the argument. Right after the
function returns 'b' is destroyed, any changes to 'b' are lost.
return true;
}
else return false;
}
I call it via a pointer to an object:

myClass* s;
toBeUpdated* updated = 0;
You probably initialise 's' to something, don't you...
>
if( s->myDataMember(a ,updated) ) {
You mean

if (s->myMethod(a, updated) ) {
cout << updated->something << endl; // SEGFAULTS!
'updated' is still null here.
}
else {
cout << "Did not return anything in 'updated'" << endl;
}
Now, the issue is that for some reason, when the method returns true,
the value assigned to b in the method does not make it out - in other
words, the value of 'updated' is not changed by calling the method.
Correct.
Am I missing a trick, or is this a bug in gcc?
There is no trick, there is no bug. You assign a new value to the
variable that exists only in 'myClass::myMet hod' function (the local
variable initialised from the argument).

If you want to change something from the outside, you need to either
pass it by reference or pass a pointer to _it_ and dereference when
assigning. So, you can do

bool myClass::myMeth od(param a, toBeUpdated* & b)
// note the & here ^^^
{
if(a == myDataMember) {
b = &someData;
return true;
}
else return false;
}
...
if (s->myMethod(a, updated) ) {
or

bool myClass::myMeth od(param a, toBeUpdated* * b)
// note the * here ^^^
{
if(a == myDataMember) {
*b = &someData;
// ^^^ Note the * here as well
return true;
}
else return false;
}
...
if (s->myMethod(a, &updated) ) {
// Note the & here ^^^

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '07 #4
Ahha! Yes, thank you :-) It works now!

Chris
Dec 4 '07 #5

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

Similar topics

6
1891
by: Bryan Martin | last post by:
I have a object that is created in a seperate domain which needs to be passed back to the parent class. Because this object is created in a seperate domain if I try to pass the object back to the parent class (different domain) I receive an error about "File Not Found". I know the object is created successfully and the objects method can be called after loading the object however upon passing it back to the calling class it exploades. I...
58
10123
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
10
6643
by: Patrick Stinson | last post by:
What sort of operations can one do with classes? I know class A{}; typeid(A); works, but what else can you do? Is there a way to pass a class as a parameter to a function? class pointers? how does this relate to class A{}; void (A::*)();
25
2926
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default construction and then.. // some other processing and/or changing 'retval' return retval; }
8
4110
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
3
4749
by: Simon Harvey | last post by:
Hi, In my application I get lots of different sorts of information from databases. As such, a lot of information is stored in DataSets and DataTable objects. Up until now, I have been passing around chunks of data in DataTables/DataSets, simply because that was the format that they were in when the data was taken from the database. Now, I know this maybe a pretty silly question with a standard "it depends" answer, but I'm going to...
4
2093
by: shade73 | last post by:
Hey all. I currently have two seperate namespaces and I'm trying to pass a connection around to them. I want to use the same connection & leave it open for 6 methods & then close it. However, all 6 of those methods use that same connection. So my solution was to pass the SqlConnection as a parameter in each method. This works, but it goes super slow. When I changed it back and put the methods all back in the same namespace & didnt...
9
3785
by: Greger | last post by:
Hi, I am building an architecture that passes my custom objects to and from webservices. (Our internal architecture requires me to use webservices to any suggestion to use other remoting techniques are not feasible) The question is; Given that I have a Person object with a private set for id. What is the recommended approac in passing that object to the web service
8
2092
by: Ivan Liu | last post by:
Hi, I'd like to ask if passing an object as an pointer into a function evokes the copy constructor. Ivan
17
7239
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
0
8893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8802
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...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
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
7405
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
6209
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
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2028
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.