Y
yccheok
Most of the time, I am using pointer as the reference to other classes.
Now, I am trying to use c++ reference to other class.
Please note that in "pad" class, it is holding a reference to "cell".
"pad"
will initialize the mycell, which is the reference (alias) to "cell"
class. I have the following question:
(1) where will the real copy of cell object stored? is it stored in the
heap memory, just like it
is being allocated through new operator.
(2) who is responsible in deleting the copy of cell aliases by mycell
reference? This is because I found that if I put the statement "delete
&mycell" in the pad destructor, memory violation will happen.
However, if I just leave it alone, I found tat the cell would not be
destructed automatically after pad is destructed
here is the output from the program:
=========
output:
cell default x called
pad default x called
pad destructor call
=========
Or should I just use back pointer? Any advice/ suggestion is very much
appreciated.
cheok
---------------------
#include <cstdio>
#include "cell.h"
cell::cell()
{
printf("cell default called\n");
}
cell::cell(const cell &t)
{
printf("cell copy called\n");
}
cell& cell:perator=(const cell &t)
{
printf("cell = called\n");
return *this;
}
cell::~cell()
{
printf("cell destructor call\n");
}
---------------------
#include <cstdio>
#include "pad.h"
pad:ad() : mycell(cell())
{
printf("pad default called\n");
}
pad:ad(const pad &p) : mycell(cell(p.GetCell()))
{
printf("pad copy called\n");
}
pad& pad:perator=(const pad &t)
{
mycell = t.GetCell();
printf("pad= called\n");
return *this;
}
pad::~pad()
{
// Nope! We cannt do that! But who will responsible for deleting
// the copy of cell which is being aliased by mycell???
//
delete &mycell;
printf("pad destructor call\n");
}
const cell& pad::GetCell() const
{
return mycell;
}
---------------------
#include "cell.h"
class pad
{
public:
pad();
pad(const pad &p);
pad& operator=(const pad &t);
~pad();
const cell& GetCell() const;
private:
cell &mycell;
};
Now, I am trying to use c++ reference to other class.
Please note that in "pad" class, it is holding a reference to "cell".
"pad"
will initialize the mycell, which is the reference (alias) to "cell"
class. I have the following question:
(1) where will the real copy of cell object stored? is it stored in the
heap memory, just like it
is being allocated through new operator.
(2) who is responsible in deleting the copy of cell aliases by mycell
reference? This is because I found that if I put the statement "delete
&mycell" in the pad destructor, memory violation will happen.
However, if I just leave it alone, I found tat the cell would not be
destructed automatically after pad is destructed
here is the output from the program:
=========
output:
cell default x called
pad default x called
pad destructor call
=========
Or should I just use back pointer? Any advice/ suggestion is very much
appreciated.
cheok
---------------------
#include <cstdio>
#include "cell.h"
cell::cell()
{
printf("cell default called\n");
}
cell::cell(const cell &t)
{
printf("cell copy called\n");
}
cell& cell:perator=(const cell &t)
{
printf("cell = called\n");
return *this;
}
cell::~cell()
{
printf("cell destructor call\n");
}
---------------------
#include <cstdio>
#include "pad.h"
pad:ad() : mycell(cell())
{
printf("pad default called\n");
}
pad:ad(const pad &p) : mycell(cell(p.GetCell()))
{
printf("pad copy called\n");
}
pad& pad:perator=(const pad &t)
{
mycell = t.GetCell();
printf("pad= called\n");
return *this;
}
pad::~pad()
{
// Nope! We cannt do that! But who will responsible for deleting
// the copy of cell which is being aliased by mycell???
//
delete &mycell;
printf("pad destructor call\n");
}
const cell& pad::GetCell() const
{
return mycell;
}
---------------------
#include "cell.h"
class pad
{
public:
pad();
pad(const pad &p);
pad& operator=(const pad &t);
~pad();
const cell& GetCell() const;
private:
cell &mycell;
};