473,471 Members | 1,981 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

is object name a pointer?

2 New Member
I'm a beginner in C++ and i'm confused about object name.
let's pretend that there's code like this:

Expand|Select|Wrap|Line Numbers
  1. class blaclass{
  2. public:
  3. void dosomething(){cout << "doing something"};
  4. }
  5.  
and i want to make a function that takes blaclass object as a parameter and returns blaclass object.and i don't know which is the right way to do it.

1.
Expand|Select|Wrap|Line Numbers
  1. blaclass veryrandomfunction(blaclass a){
  2. a.dosomething()
  3. blaclass blaobj2;
  4. return blaobj2;
  5. }
  6.  

2.
Expand|Select|Wrap|Line Numbers
  1. blaclass& veryrandomfunction(blaclass& a){
  2. a.dosomething()
  3. blaclass blaobj2;
  4. return blaobj2;
  5. }
  6.  
is way 1 right or is way 2 right? in other terms, is object name(identifier) a pointer?
Feb 24 '07 #1
2 1664
veryrandomname
2 New Member
problem solved
i didn't know the c++ feature that you can pass '&' after the parameter type to use it as a reference.
if what i remember is right, C didn't have that
yea, C++ is kewl.
Feb 24 '07 #2
Ganon11
3,652 Recognized Expert Specialist
Just for the sake of my own satisfaction...

I'm fairly sure that declaring the object inside a function is not the right way to go. It becomes a local variable that (I think?) will become deallocated after the function ends, so while you may be returning the address of the object, that address will be meaningless after the function, as the memory there is deallocated.

Instead, you should use a pointer to create an object in memory that will NOT be deallocated after the function - you can then return the pointer, or the value of the pointer (i.e. the object), and the object will still exist.
Feb 25 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
5
by: August1 | last post by:
This is a short program that I have written from a text that demonstrates a class object variable created on the stack memory and another class object variable created on the heap memory. By way...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
11
by: jacob navia | last post by:
I am writing software to make a general storage facility of any kind of objects to/from disk. The intermeidate format used is XML, using the schema (modified a bit) of Microsoft:...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
6
by: Martin | last post by:
Hi I need to maintain a <setof pointers to objects, and it must be sorted based on the values of pointed objects, not pointer values. I can achieve this easily by defining my own comparing...
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
275
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'
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
1
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.