Sep 17, 2009 at 4:25pm UTC
Hey,
I would like to copy the data from one class "Point" to another, however the class contains a pointer to an object of class "Line" in an array, which is not visible is class "Point". How can I make it more beautiful than reading out the data *outside* class "Point" and then reinserting it, since outside of class "Point" I can access the array of class "Line". The lines in question are in
"void HierModel::divide(int lvl)" line 13ff
Any idea? Thanks
Steffen
P.S. The code compiles and runs perfectly with g++.
::::::::::::::
global.h
::::::::::::::
#ifndef _HIERMODEL
#define _HIERMODEL 1
using namespace std;
class Point;
class Line;
class Hiermodel;
#include "point.h"
#include "line.h"
#include "hiermodel.h"
#endif /*ifndef _HIERMODEL */
::::::::::::::
hiermodel.h
::::::::::::::
class HierModel {
int numPnts;
Point *points;
int numLines;
Line *lines;
public:
HierModel();
~HierModel();
void init(double, double);
void divide(int);
int linesAtLevel(int);
};
::::::::::::::
line.h
::::::::::::::
class Line {
Point *begin,*end;
int level;
int name;
public:
void init (int, int);
void define (Point*,Point*);
void set(int tname=-1, int tlevel=-1, Point* tbegin=0, Point* tend=0);
void get(int*, int*, int*, int*);
int getName() const {return name;}
int getLevel() const {return level;}
int getBegin() const;
int getEnd() const;
double beginGetX() const;
double endGetX() const;
double centerX() const;
};
::::::::::::::
point.h
::::::::::::::
class Point {
Line *line;
double x;
Point *partner;
int level;
int name;
public:
Point();
void init (int, int);
void setL0 (double);
void setPartner (Point*);
void set(int tname=-1, int tlevel=-1, double xcoord=0, Line *tline=0);
void setLine(Line *tline);
int getPartner() const {return partner->getName();}
int getLine() const;
int getName() const {return name;};
int getLevel() const {return level;};
double getX() const {return x;}
void get(int*, int*, double*);
};
::::::::::::::
hiermodel.cpp
::::::::::::::
#include "global.h"
HierModel::HierModel(){
numPnts=0;
numLines=0;
points=0;
lines=0;
}
HierModel::~HierModel(){
delete[] points;
delete[] lines;
}
void HierModel::init(double start, double end) {
numPnts = 2;
points=new Point[numPnts];
points[0].init(0,0);
points[1].init(1,0);
points[0].setL0 (start);
points[1].setL0 (end);
points[0].setPartner(&points[1]);
points[1].setPartner(&points[0]);
numLines= 1;
lines=new Line[1];
lines[0].init(0,0);
lines[0].define (&points[0],&points[1]);
}
int HierModel::linesAtLevel(int lvl){
int number=0;
for(int i=0; i<numLines;i++){
if( lines[i].getLevel() == lvl ) number++;
}
return number;
}
void HierModel::divide(int lvl) {
Point *tempPoints;
Line *tempLines;
int newPnts,newLines;
int tempName,tempLevel,tempBegin,tempEnd;
double tempX;
Line *tempLine;
int newNumber=linesAtLevel(lvl);
newPnts=numPnts+newNumber*2;
tempPoints=new Point[newPnts];
newLines=numLines+newNumber*2;
tempLines=new Line[newLines];
for(int i=0; i<numPnts;i++){
points[i].get(&tempName,&tempLevel,&tempX); //copy data (get data)
tempPoints[i].set(tempName,tempLevel,tempX);//copy data (set it)
tempPoints[i].setPartner( &tempPoints[ points[i].getPartner() ] ); //copy set Point pointer
if(tempLevel!=0) {
tempPoints[i].setLine( &tempLines[ points[i].getLine() ] ); //copy LINE pointer
}
}
for(int i=0; i<numLines;i++){
lines[i].get(&tempName,&tempLevel,&tempBegin,&tempEnd);
tempLines[i].set(tempName,tempLevel, &tempPoints[tempBegin], &tempPoints[tempEnd] );
}
int n=0;
for(int i=0; i<numLines ;i++){
if( tempLines[i].getLevel() == lvl ) {
double s=tempLines[i].centerX();
int j1=numPnts+2*n;
int j2=numPnts+2*n+1;
tempPoints[j1].set(j1,lvl+1,s,&tempLines[i]);
tempPoints[j2].set(j2,lvl+1,s,&tempLines[i]);
int tbegin=tempLines[i].getBegin();
int tend =tempLines[i].getEnd();
tempPoints[j1].setPartner(&tempPoints[tbegin]);
tempPoints[j2].setPartner(&tempPoints[tend]);
int l1=numLines+n*2;
int l2=numLines+n*2+1;
tempLines[l1].set(l1,lvl+1,&tempPoints[tbegin], &tempPoints[j1] );
tempLines[l2].set(l2,lvl+1,&tempPoints[j2] , &tempPoints[tend]);
n++;
}
}
numPnts=newPnts;
numLines=newLines;
delete points;
delete lines;
points=tempPoints;
lines=tempLines;
return;
}
::::::::::::::
line.cpp
::::::::::::::
#include "global.h"
void Line::init (int n,int l){
name=n;
level=l;
return;
}
void Line::define (Point *a, Point *b){
begin=a;
end=b;
return;
}
void Line::set (int tname, int tlevel, Point* tbegin, Point* tend) {
if(tname!=-1) name=tname;
if(tlevel!=-1) level=tlevel;
if(tbegin!=0) begin=tbegin;
if(tend!=0) end=tend;
return;
}
void Line::get (int *tname, int *tlevel, int* tbegin, int* tend) {
*tname=name;
*tlevel=level;
*tbegin=begin->getName();
*tend =end->getName();
return;
}
int Line::getBegin() const {
return begin->getName();
}
int Line::getEnd() const {
return end->getName();
}
double Line::beginGetX() const {
return begin->getX();
}
double Line::endGetX() const {
return end->getX();
}
double Line::centerX() const {
return ( begin->getX() + end->getX() )/2.;
}
::::::::::::::
main.cpp
::::::::::::::
#include "global.h"
int main () {
HierModel *hm=new HierModel;
// Initialize
hm->init(0.,1.);
// divide
for (int i=0;i<5;i++){
hm->divide(i);
}
delete hm;
return 0;
}
::::::::::::::
point.cpp
::::::::::::::
#include "global.h"
Point::Point(){
x=0;
level = -1;
name = -1;
partner = 0;
line=0;
}
void Point::init(int n, int lev){
level = lev;
name = n;
}
void Point::setL0 (double xcoord) {
x = xcoord;
return;
}
void Point::setPartner (Point *other) {
partner = other;
return;
}
void Point::set (int tname, int tlevel, double xcoord, Line *tline) {
if(tname!=-1) name=tname;
if(tlevel!=-1) level=tlevel;
x = xcoord;
if(tline!=0) {
line=tline;
}
return;
}
void Point::setLine(Line *tline){
line=tline;
}
int Point::getLine() const {
return line->getName();
}
void Point::get (int *tname, int *tlevel, double *tx) {
*tname=name;
*tlevel=level;
*tx = x;
return;
}