CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   Converting objects (http://www.cfd-online.com/Forums/openfoam/60955-converting-objects.html)

shuo November 6, 2006 17:37

Say a have two classes: cla
 
Say a have two classes:

class A {

double variable[30];

public:


void show_variable();

};
and

class B {

double variable[30];

public:

void show_variable();

};

How to I convert from one to another so that when equate them A = B, variable[30] of class A = variable[30] of class B?

Regards

Shuo

brooksmoses November 6, 2006 17:51

Define an assignment operator.
 
Define an assignment operator. I think the following will work, but it's not at all tested.

class B{
double variable[30];
public:
show_variable();
B& operator= (const A&);
};

B& B::operator= (const A& objA) {
for (int i=0; i<30; i++)
variable[i] = objA.variable[i];
return *this;
};

Of course, that also needs to have access to A's internals, so you declare it as a friend of A:

class A{
double variable[30];
public:
void show_variable();
friend B& B::operator= (const A&);
};


All times are GMT -4. The time now is 09:07.