473,405 Members | 2,300 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,405 software developers and data experts.

heap based objects

Hello Experts!

I know that to use heap-based objects, the C++ programmer has to define
variables that are of pointer types like Car *car = new Car;

Now to my question I also read this "to use heap-based objects, the C++
programmer has to define variables that are of either pointer or reference
types."

Is it really possible to use heap-based object when having variables of
reference types.

How does that statement look like if that is possible.

//Tony
Aug 11 '05 #1
5 1872
Tony Johansson wrote:
Hello Experts!

I know that to use heap-based objects, the C++ programmer has to define
variables that are of pointer types like Car *car = new Car;

Now to my question I also read this "to use heap-based objects, the C++
programmer has to define variables that are of either pointer or reference
types."

Is it really possible to use heap-based object when having variables of
reference types.

How does that statement look like if that is possible.

//Tony


Well, think about dereferencing a pointer, perhaps from the return of
another function.

Car test()
{
Car* car = new Car;
return *car;
}

Also, you could do the same for parameters.

For test(Car& car)

You could call it with test(*pCar) and it would never know the
difference.

The former I've used very very rarely (if at all, can't remember), but
you never know when you might need these things.

HTH,
Josh McFarlane

Aug 11 '05 #2

Josh Mcfarlane wrote:
Tony Johansson wrote:
Hello Experts!

I know that to use heap-based objects, the C++ programmer has to define
variables that are of pointer types like Car *car = new Car;

Now to my question I also read this "to use heap-based objects, the C++
programmer has to define variables that are of either pointer or reference
types."

Is it really possible to use heap-based object when having variables of
reference types.

How does that statement look like if that is possible.

//Tony
Well, think about dereferencing a pointer, perhaps from the return of
another function.

Car test()


Car& test()
{
Car* car = new Car;
return *car;
}


Note that if a function allocates dynamic memory, you should return a
pointer to that dynamic memory, never a reference. With a reference, it
is assumed that the function will clean up the memory in some way. With
a pointer, any decent programmer will at least look at the function's
documentation to know whether or not the caller needs to clean-up the
memory. Or better yet, use a std::auto_ptr or another smart pointer
class to make ownership clear.

Aug 11 '05 #3
Alipha wrote:
Note that if a function allocates dynamic memory, you should return a
pointer to that dynamic memory, never a reference. With a reference, it
is assumed that the function will clean up the memory in some way. With
a pointer, any decent programmer will at least look at the function's
documentation to know whether or not the caller needs to clean-up the
memory. Or better yet, use a std::auto_ptr or another smart pointer
class to make ownership clear.


True, the example that came to my mind involved allocating in the
constructor and then deallocating in the destructor, and then returning
references, but returning the pointer is normally alot more useful. Was
just trying to give an easy example. =)

Josh McFarlane

Aug 11 '05 #4
Tony Johansson wrote:
I know that, to use heap-based objects,
the C++ programmer [must] define variables
that are of pointer types like Car *car = new Car;

Now to my question, I also read this,
"to use heap-based objects, the C++ programmer [must] define variables
that are of either pointer or reference types."

Is it really possible to use heap-based object
when having variables of reference types.

How does that statement look like if that is possible. cat main.cc #include <iostream>
#include <string>

class Car {
private:
// representation
std::string Make;
std::string Model;
public:
// functions
std::string make(void) const {
return Make;
}
std::string model(void) const {
return Model;
}
// operators
friend
std::ostream& operator<<(std::ostream& os, const Car& car) {
return os << car.make() << ' ' << car.model();
}
// constructors
Car(const std::string& make = "Chevrolet",
const std::string& model = "Corvette"):
Make(make), Model(model) { }
};

int main(int argc, char* argv[]) {
const Car& car = *(new Car);
std::cout << car << std::endl;
delete &car;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main

Chevrolet Corvette
Aug 11 '05 #5

Tony Johansson schreef:
Hello Experts!

I know that to use heap-based objects, the C++ programmer has to define
variables that are of pointer types like Car *car = new Car;

Now to my question I also read this "to use heap-based objects, the C++
programmer has to define variables that are of either pointer or reference
types."

Is it really possible to use heap-based object when having variables of
reference types.


Easily. The reason is that C++ supports inheritance. A Car* can hold a
(new Car) but also a (new SportsCar). Now, if you want to pass both
to a function, the function shouldn't try to copy the Car. If it copies
a Car, it copies only the Car parts and loses the SportsCar extras.
A function that takes a Car& will not copy the Car, so it will work on
the original SportsCar.

HTH,
Michiel Salters

Aug 12 '05 #6

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

Similar topics

4
by: Shailesh Humbad | last post by:
If a class has no constructor/destructor, is not part of an inheritance heirarchy, is not a template, and only contains members of integral types, then is it okay to allocate it off the heap? Is...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
3
by: balu | last post by:
Hi Gurus I need a help. Im developing a C++ libaray. I don't whether user creates object in stack/heap. Is there a way to find object location? Balu.N
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
4
by: MattAhsworth | last post by:
I'm develpoing a mapping app and want to store the point data that makes up lines, points and polygons efficiently. Am I better off using unsafe C# and creating structs to hold the data on the...
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
2
by: openuser | last post by:
Hi, I'm just wondering: when I have class A which owns a non-heap based vector which stores pointers to heap based object B (these objects are also owned by class A), and the object instance of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
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
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
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...

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.