how to use operator overloading with heap memory allocation.

T

toton

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+image2 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
 
V

Victor Bazarov

toton said:
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
 
W

wkaras

toton said:
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+image2 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.
 
T

toton

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top