473,413 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

static_cast, inheritance and more.

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.
Jul 23 '05 #1
2 1942
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

Alf P. Steinbach wrote:
* 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'.


Yes, thats a typo. It 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.
I need to start inserting somewhere from the Start, and thats why I 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.

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.


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.

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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Gary Labowitz | last post by:
Am I doing this correctly? It is a sample program for my class. #include <iostream> using namespace std; int main( ) { int x=3, y=4;
9
by: news.ir.com.au | last post by:
Hi, In the following code I get the compiler error: error C2243: 'static_cast' : conversion from 'class B *' to 'class A *' exists, but is inaccessible I understand why I get this error and...
26
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would...
3
by: Kobe | last post by:
Hi, if I need to convert a size_t to an int, in "older" C++ I'd write the following code (using C-like "casting"): <CODE> std::vector<...> v; int count = (int) v.size(); // v.size() returns...
6
by: Dave Rahardja | last post by:
Is it safe to use static_cast to downcast from a virtual base class? For example, class V {}; class A: public virtual V {}; class B: public virtual V {}; class C: public A, public B; void...
24
by: Rahul | last post by:
Hi, I have a class A : public B {...member functions......data members}; and am doing the following A *p=new A(); void *p=static_cast<void *>(p); factory_instance->process(p);
5
by: Grizlyk | last post by:
I found, that I must write explicit cast from non-const reference to const reference. I have a class with member returning non-const reference to itself template<class A,class B,class C>...
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
3
by: Rahul | last post by:
Hi, Everywhere I read that static_cast<only work fine for the conversion which are implicitly allowed by the compiler hence the following does not work int *i; double *d; d = i; ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.