A
Amit
Greetings.
I am having some problem while using a cast operation(static_cast and/or
dynamic_cast) between base and derived objects when passing to functions.
what I have is something like this..
template <class T>
class Node{
protected:
T e;
Node* left;
Node* right;
public:
//....
virtual ~Node() { }
friend class MYObjects;
};
template<class T>
class MyObjects
{
protected:
Node<T>* Start;
void Add(Node<T>*& p, const T& elem);
//...
public:
virtual void Add(const T& elem); // which calls the protected Add above with
the Start
//...
};
template<class T>
class TNode: public Node<T>
{
private:
int something;
//...
public:
//...
};
template<class T>
class MyTnodeObjects : public MyObjects<T>
{
protected:
void Add(TNode<T>*& p, const T& elem);
//...
public:
virtual void Add(const T& elem); // which of course calls the protected Add
above in this class with the Start pointer in the base Node class.. The
Start will of course be created via TNode if TNodeobjects are used
//...
};
template<typename T>
void MyTnodeObjects<T>::Add(const T& elem)
{
Insert(static_cast<TNode<T>* &>(Start), elem); // problem in the
code....
}
The above statement where I have said Problem in the code...
1>I cant compile that statement, as the compiler gives an error regarding
type conversion. So, how do I create a reference to a pointer using
static_cast.
2>Second, if I just use static_cast<TNode<T>* >(Start) and then send that by
reference, then static_cast would make a copy of the pointer which would be
temporary(Does static_cast make that pointer or inherently, the function
call induces that, I dont know) and the reference to that would be sent,
which is not what I want as I want to send the reference to Start and of
course, since a temporary is created, you cannot bind it to a non-constant
reference or a reference to a constant pointer.
3>what is the difference between static_cast<TNode<T>* &>(Start) and
static_cast<TNode<T>*>(Start). Shouldnt the compiler see that the original
pointer needs to be sent by reference ?
4>To get away with all this, I used reinterpret_cast and everything worked.
So, is the following safe and practical and complaint ?
reinterpret_cast<TNode<T>* &>(Start) ;
Thanks a bunch.
I am having some problem while using a cast operation(static_cast and/or
dynamic_cast) between base and derived objects when passing to functions.
what I have is something like this..
template <class T>
class Node{
protected:
T e;
Node* left;
Node* right;
public:
//....
virtual ~Node() { }
friend class MYObjects;
};
template<class T>
class MyObjects
{
protected:
Node<T>* Start;
void Add(Node<T>*& p, const T& elem);
//...
public:
virtual void Add(const T& elem); // which calls the protected Add above with
the Start
//...
};
template<class T>
class TNode: public Node<T>
{
private:
int something;
//...
public:
//...
};
template<class T>
class MyTnodeObjects : public MyObjects<T>
{
protected:
void Add(TNode<T>*& p, const T& elem);
//...
public:
virtual void Add(const T& elem); // which of course calls the protected Add
above in this class with the Start pointer in the base Node class.. The
Start will of course be created via TNode if TNodeobjects are used
//...
};
template<typename T>
void MyTnodeObjects<T>::Add(const T& elem)
{
Insert(static_cast<TNode<T>* &>(Start), elem); // problem in the
code....
}
The above statement where I have said Problem in the code...
1>I cant compile that statement, as the compiler gives an error regarding
type conversion. So, how do I create a reference to a pointer using
static_cast.
2>Second, if I just use static_cast<TNode<T>* >(Start) and then send that by
reference, then static_cast would make a copy of the pointer which would be
temporary(Does static_cast make that pointer or inherently, the function
call induces that, I dont know) and the reference to that would be sent,
which is not what I want as I want to send the reference to Start and of
course, since a temporary is created, you cannot bind it to a non-constant
reference or a reference to a constant pointer.
3>what is the difference between static_cast<TNode<T>* &>(Start) and
static_cast<TNode<T>*>(Start). Shouldnt the compiler see that the original
pointer needs to be sent by reference ?
4>To get away with all this, I used reinterpret_cast and everything worked.
So, is the following safe and practical and complaint ?
reinterpret_cast<TNode<T>* &>(Start) ;
Thanks a bunch.