static_cast, inheritance and more.

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.
 
A

Alf P. Steinbach

* Amit:
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....
}

Presumably the 'Insert' should be 'Add'.

The above statement where I have said Problem in the code...

The actual problem is much earlier, in the decision to have that parameter
at all.

Why don't you make the node type a template parameter of class MyObjects,
defaulting to Node<T>?

Or even better, just use a std::list.

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.

Don't. Start is not a pointer to a TNode.

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) ;

That's Unspecified Result.
 
A

amparikh

Alf said:
* Amit:

Presumably the 'Insert' should be 'Add'.

Yes, thats a typo. It should be add.
The actual problem is much earlier, in the decision to have that parameter
at all.
need that parameter. or else how would the TNodeObjects container start
the search for an insert ?
Why don't you make the node type a template parameter of class MyObjects,
defaulting to Node<T>?

I am not sure what you mean here, but MyObjects and MyTNodeObjects
could have different characteristics and both could be instantiated and
thats why I need them seperately.
Or even better, just use a std::list.



Don't. Start is not a pointer to a TNode.

Not necessarily...
If I only use TNodes, then the actual Node is a TNode and therefore
Start is a Tnode.

Tnode(int elem = T(), left* l = NULL, right* r = NULL) : Node<T>(elem,
l, r) //

so when I do a new TNode and if it is the first node of the container
MyTnodeObjects, then the underlying pointer that Start will be
initialzed with will be actually pointing to a TNode.

Therefore I should be able to do the cast.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top