blah
F test = getF();
// Print out shows that an F object was destroyed by the ~F. F set
var to 0 on delete;
test.print();
//reveals that var is still 123
value is 123 still instead of 0
F getF()
{
F f;
//default is 0;
f.setVar(123);
return f;
}
I thought since I created this on the stack instead of the heap that I
would run into problems. I have not overloaded anything. I thought
the best way would be to return a pointer to the heap and create the
object on the heap as well.
What did I miss, and why does this work? I thought that I had to do
some overloading to make data copy as well. 1 1170
opistobranchia wrote: blah F test = getF(); // Print out shows that an F object was destroyed by the ~F. F set var to 0 on delete; test.print(); //reveals that var is still 123 value is 123 still instead of 0
F getF() { F f; //default is 0; f.setVar(123); return f; }
I thought since I created this on the stack instead of the heap that I would run into problems. I have not overloaded anything. I thought the best way would be to return a pointer to the heap and create the object on the heap as well.
What did I miss, and why does this work? I thought that I had to do some overloading to make data copy as well.
getF() returns an 'F' object, not a ref or pointer to an 'F', but
a full 'F' object. So, a COPY of the 'F' created inside getF() is
returned, then 'test' is initialized from that copy. The 'F' created
inside getF() is destroyed when getF() returns, but the (unnamed)
copy created from it is returned from getF(), used to init 'test', then
deleted.
Larry This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: pmatos |
last post by:
Hi all,
Sometimes I have a function which creates an object and returns it.
Some are sets, other vectors but that's not very important. In these
cases I do something like this:
vector<int> * f() {
vector<int> * v = new vector<int>;
return v;
}
|
by: IMSHURKKPWII |
last post by:
Hi all,
I am confused about the methods by which C passes things to other
routines. If I have a routine,
void rt( , , ...);
Then I know that the process to call this function is this:
1. Push values onto the stack, from right to left.
|
by: anonymous |
last post by:
Thanks your reply.
The article I read is from
www.hakin9.org/en/attachments/stackoverflow_en.pdf.
And you're right. I don't know it very clearly. And that's why I want
to understand it; for it's useful to help me to solve some basic
problem which I may not perceive before.
I appreciate your help,
sincerely.
|
by: RCS |
last post by:
I have a UI that needs a couple of threads to do some significant processing
on a couple of different forms - and while it's at it, update the UI (set
textboxes, fill in listviews). I created a base class for the worker class,
and made up some functions/delegates to handle the invoke stuff for the UI
and that was fine for a prototype. I rewrote this chunk, broke things out
into different classes - but the threading is still the same - and...
|
by: web1110 |
last post by:
If I have 2 variables, A and B, referencing the same object and then do a
A.Dispose(), what happens to B?
| |
by: krishna81m |
last post by:
In the following code snippet, I created a class samp which has two private variables /char/ and /char*/ which are initialized in the constructor. I create a function /samp input()/ that returns an object of this class and see that the object is deleted after returning from the function. However, I note that unless I write my own destructor, the object is not being deleted even it is out of scope. I also have a global variable samp* GlobSampObj;...
|
by: jackchang1 |
last post by:
It's not difficult getting the function name of the caller but if the
caller is an object's method, how do you get the method name?
<html>
<head>
<script type="text/javascript">
function displayCallStack(func){
var caller = func.caller;
|
by: Tarique |
last post by:
Hello all.I am trying to implement a stack which can store either
integer or float values.
The code is given below:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STACKSIZE 100
|
by: Astley Le Jasper |
last post by:
Sorry for the numpty question ...
How do you find the reference name of an object?
So if i have this
bob = modulename.objectname()
how do i find that the name is 'bob'
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |