473,887 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to use operator overloading with heap memory allocation.

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 memory allocation?
like complex<int> * c1,*c2,*c3;
i still want to do something like c3 = c1+c2 ; rether than *c3 =
*c1+*c2;

can a third operator overloading be used to convert pointer to
reference?...
or is there exist some special pointer class where +,- etc wont add the
pointers itself but rather the data.
and when to use class & data both in stack , the class in stack & data
in heap (like c++ vector etc) and both class & data in heap? any
general guideline?
It seems that all of the gui classes initialize in heap ( like Button
Panel etc) or data centric classes like Image File etc.
I want something like , i have an Image class ( say TImage from borland
) which should be initialized as TImage* image=new TImage(filename ); as
it creates the class at runtime in heap.
now i want to use some sorthand operator like >> to store it in a file
( opt stream) or +,- etc for image operations line
image3=image1+i mage2 rather than *image3 = *image1+*image2 ;
note i cant initialize TImage img, as it cant call default ctor which
needs filename, but that available only when user inputs it. also if i
add a default ctor then the state of the image is unknown.
In sort can any pointer trick can be performed to look a c++ heap
based class look like Java heap based class , where it internally
converts the pointer as reference , and hence everyting can be accessed
using . (dot ) operator.
thanks
abir basak

Apr 24 '06 #1
3 3478
toton wrote:
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 memory allocation?
It cannot.
like complex<int> * c1,*c2,*c3;
i still want to do something like c3 = c1+c2 ; rether than *c3 =
*c1+*c2;
We feel your pain.
can a third operator overloading be used to convert pointer to
reference?...
Huh?
or is there exist some special pointer class where +,- etc wont add
the pointers itself but rather the data.
I wouldn't be a pointer in that case. + and - applied to a pointer
in C++ mean indexing.
and when to use class & data both in stack , the class in stack & data
in heap (like c++ vector etc) and both class & data in heap? any
general guideline?
General guideline: don't do that.

You can, of course, once you obtained your pointers to the dynamic
objects, create references to the same objects by dereferencing the
pointers, and then use those references in the expressions:

complex<int> *pc1, *pc2, *pc3;
... // allocate the objects
complex<int> &c2, &c3, &c3;
c3 = c1 + c2;
...

Keep in mind, when you dispose of 'pc1', etc., your references become
invalid.
[..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 24 '06 #2
toton wrote:
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 memory allocation?
like complex<int> * c1,*c2,*c3;
i still want to do something like c3 = c1+c2 ; rether than *c3 =
*c1+*c2;

can a third operator overloading be used to convert pointer to
reference?...
or is there exist some special pointer class where +,- etc wont add the
pointers itself but rather the data.
and when to use class & data both in stack , the class in stack & data
in heap (like c++ vector etc) and both class & data in heap? any
general guideline?
It seems that all of the gui classes initialize in heap ( like Button
Panel etc) or data centric classes like Image File etc.
I want something like , i have an Image class ( say TImage from borland
) which should be initialized as TImage* image=new TImage(filename ); as
it creates the class at runtime in heap.
now i want to use some sorthand operator like >> to store it in a file
( opt stream) or +,- etc for image operations line
image3=image1+i mage2 rather than *image3 = *image1+*image2 ;
note i cant initialize TImage img, as it cant call default ctor which
needs filename, but that available only when user inputs it. also if i
add a default ctor then the state of the image is unknown.
In sort can any pointer trick can be performed to look a c++ heap
based class look like Java heap based class , where it internally
converts the pointer as reference , and hence everyting can be accessed
using . (dot ) operator.
thanks
abir basak


I don't understand alot of what you're suggesting. Maybe this
template:

template <typename T>
class Dyn
{
private:
T *p;
public:
Dyn() : p(0) { }
Dyn(const T &t) : p(new T) { *p = t; }
Dyn(const Dyn &d) : p(new T) { *p = *d.p; }
const T & operator () const { return(*p); }
T & operator () { return(*p); }
// Non-chainable assignent.
void operator = (const T &t)
{
if (p) delete p;
*p = t;
}
~Dyn() { if (p) delete p; }
};

is along the lines of what you're looking for. But I wouldn't
recommend using
it except to experiment with it. If instances of a class really should
always
store their data in the heap, that should be designed into the class
implementation. For classes whose instances are sometimes in the
heap, sometimes not, manage the in-heap instances using STL/Boost
templates (such as "vector", "auto_ptr" or other smart pointers). If
you
are really convinced that it's better for all instances of all
user-defined
types to be in the heap, it's probably makes more sense to just
use Java. I think C# may put all UD type instances in the heap too.

Apr 24 '06 #3
In Java everything is initialized in heap (hence the term gc came into
exsistence) and they need to be deallocated just like c++ new & delete,
however it is done by jvm. In C# class is heap based while struct is
stack based. Here the syntax are clear in both case. heap based means
both the class & data are initialized in heap, and hence need to be
deleted.
while in case of stack, both the class & data are on the stack.

thus in Java
class X{
public int[] x;
}
here both X & x(member variable) are initialized in heap. My question
is what happes in C++?
if I use X p; it is clear that X is in stack. is x is also in stack?
or for
class X{
public:
int* x;
}
X p; creates X in stack, but x in heap, (hence needs a destructor to be
called).
My question is when to use which one? If a class to be initialized in
stack, will i make all fields also stack based?

About the pointer based operator overloading, it is unfortunate that
C++ mixes pointer arithmatic & ordinary arithmatic with same operator.
(C++ has a bad habit to do so. People say pointer is hard may be
because only * and & has 3 different meanings, it is evenharder to
design for a compiler writer like me:) ).
However what i want to know that can it be done something like,
my_ptr pc(new complex<int>(2, 3));
and then use pc3 = pc1+pc2; in ordinary way., where + operator for
my_ptr is overloaded to return reference to the complex class rather
than a pointer?

An additional question,
How c++ array delete [] var ; knows the size of the array, which it
doesnt store anyway?

May 8 '06 #4

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

Similar topics

11
912
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
0
1652
by: Robert Potthast | last post by:
Hello, I want to make my garbage collector more safe. To make it more safe I need to know if an object has been allocated on the stack or on the heap using the operator new. My garbage collector uses a mixture of reference counting and smart pointers. I have got a base class ("Object") which handles all the memory management stuff. I have worked out different approaches to pass the info to my base class (won't name all):
7
4681
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++ copyconstructor1.cpp #copyconstructor1.cpp: In function `int main()': #copyconstructor1.cpp:86: no match for `sample& = sample' operator #copyconstructor1.cpp:53: candidates are: sample sample::operator=(sample&) ...
5
4477
by: Niall | last post by:
I have an unmanaged C++ ray tracer which I am working to put a C# front end on. It runs fine as just the unmanaged code. I have made a MC++ wrapper DLL to expose the required types to the C# project. Things seem to be getting through from the unmaged side to the managed side - the image is being rendered correctly. However, at random points, calls to new are returning null. Hence, when the managed type gets destroyed and calls delete on its...
16
4462
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 in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
3
1679
by: Abubakar | last post by:
Hi, lets say I have a class called "hashstring". I want to be able to write the following code: hashstring hs ( "hello world" ); char * somecharptr; somecharptr = hs; // here the somecharptr starts pointing to "hello world". is it possible? How do I overload assignment operator such that I'm able to
2
2996
by: lovecreatesbea... | last post by:
If the built-in operator keyword new doesn't allocate memory on heap and it calls global operator new (::operator new) or class member operator new to do that. What are the two kinds of operator new used to allocate heap memory? Before there aren't global and member operator new, the built-in operator keyword new does really allocate heap memory, right?
12
2355
by: y-man | last post by:
Hi, I am creating a child class of the vector, which contains a couple of functions to make the work with carthesian coordinate vectors easier. To make things work more easily, I would like to be able to access the vector through a string which is either x, y or z. So that I can use: ---xyzvec.hh-- #ifndef XYZVEC_HH #define XYZVEC_HH
13
5055
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one implement operator new/delete I can't see a way to indicate, on delete, how many objects must be destroyed (or how big the space is) - alternatively I can't figure out what are the alignment requirements so that the implementation, after calling my...
0
9957
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
11173
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
10770
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
10875
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
10432
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...
1
7988
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
5809
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
4632
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
4238
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.