![]() |
| | #1 |
| Algorithm engineer Join Date: Jun 2006
Posts: 223
| Clone an object in a polymorphic class? In a lab we are doing in school we are going to create an expression tree, looking for example like this when it's printed: Code: 5
/
*
\
7
/
+
\
6
node_operator in turn, is an abstract class which have subclasses called node_plus, node_minus, node_times etc. Each object which is of a derived class from node_operator will have two and only two data members, left_node and right_node. To clone a object from any subclass of node_operator, it will be the exact same code: Code: node_x* node_x::clone() {
return new node_x(left_child->clone(), right_child->clone());
}
Code: virtual node_operator* clone();
...
virtual node_operator* node_operator::clone(){
return new typeof(this*) (left_child->clone(), right_child->clone());
}
__________________ Come on, you can do it! b( ~_') |
| TriKri is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,259
| There is no typeof in standard C. Have you learned about templates yet? Maybe you could template it. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #3 |
| Registered User Join Date: Jun 2008
Posts: 1,134
| |
| C_ntua is offline | |
| | #4 |
| Registered User Join Date: Jun 2008 Location: RING 0
Posts: 462
| Sounds like a good use of the prototype pattern. Prototype Design Pattern There is the UML of how you could design your class structure. Yes each class needs to know "how" to copy itself. |
| valaris is offline | |
| | #5 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Are you really supposed to print that for the expression tree? Why? The expression tree would be this: Code: +
/ \
/ \
* 6
/ \
/ \
5 7
|
| tabstop is offline | |
| | #6 |
| Algorithm engineer Join Date: Jun 2006
Posts: 223
| Expression trees can be displayed in many ways, it can also look like this: Code: -- 5
|
-- *
| |
| -- 7
|
-- +
|
-- 6
__________________ Come on, you can do it! b( ~_') |
| TriKri is offline | |
| | #7 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
As to that, C stole typeof from C++'s typeid, so that shouldn't really be a problem. | |
| tabstop is offline | |
| | #8 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| This is definitely a candidate for templatization. Code: template < typename Op >
class BinaryNode
{
public:
BinaryNode( Node *lhs, Node *rhs )
: mLhs( lhs ),
mRhs( rhs )
{
}
Value Evaluate() const
{
return Op::Evaluate( lhs->Evaluate(), rhs->Evaluate() );
}
BinaryNode *Clone() const
{
return new BinaryNode( mLhs->Clone(), mRhs->Clone() );
}
private:
Node *mLhs;
Node *mRhs;
};
Code: class AdditionOperator
{
public:
static Value Evaluate( Value lhs, Value rhs )
{
return lhs + rhs;
}
};
Code: typedef BinaryNode< AdditionOperator > AdditionNode; (My code is not exception safe. In general, none of the examples I give are exception safe unless otherwise claimed)
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #9 |
| Algorithm engineer Join Date: Jun 2006
Posts: 223
| Thank you brewbuck, I'll remember that next time I get in the same situation! -Kristofer
__________________ Come on, you can do it! b( ~_') |
| TriKri is offline | |
![]() |
| Tags |
| c++, cloning, polymorphism, typeof, virtual functions |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting an error with OpenGL: collect2: ld returned 1 exit status | Lorgon Jortle | C++ Programming | 6 | 05-08-2009 08:18 PM |
| Inventory tracking of dynamically allocated items | Mario F. | C++ Programming | 11 | 07-23-2006 05:39 PM |
| Mmk, I give up, lets try your way. (Resource Management) | Shamino | Game Programming | 31 | 01-18-2006 09:54 AM |
| My Window Class | Epo | Game Programming | 2 | 07-10-2005 02:33 PM |
| C++ Class Object Collection | Visual Develope | C++ Programming | 3 | 05-04-2002 04:48 PM |