Solved

C++ : Casting from Template to a Base Class pointer... How?

Posted on 2013-05-23
3
593 Views
Last Modified: 2013-05-24
Here is a way I set up my design

class Master is a "interface".....

class Master{

      ~Master(){}

      virtual void setSomeThing(int aValue) = 0;
};

Class Slave is a template derived from Master.......

template <class PositionType, int Positionize>
class Slave : public Master
{
   Slave (std::string name, bool someValue)
   {
   }

   /* Overrides....*/
   void setSomeThing(int aValue) {/** Do sometthing here **/}
}

Note : PositionType is designed to take any enum type.

So in a main() I do the following...

main()
{

 Master* aMaster = new Slave <SomeType, someSize>("aName" ,false);
                                          /** SomeType is a enum type **/
 /** The above instantiation compiles and work good...**/
 
 //And then  I want the above object back to a Salve pointer....
 //So I try this,...
 
 Slave <SomeType, someSize>* convertback = (Slave <SomeType, someSize>) aMaster;
//Cannot do this. error.

}

So my question is.. how to cast the Master pointer (which is pointing to a Slave object) back to a Slave pointer?
0
Comment
Question by:prain
3 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 200 total points
ID: 39192515
Looks like you are just missing a *

Slave <SomeType, someSize>* convertback = (Slave <SomeType, someSize>*) aMaster
0
 

Author Closing Comment

by:prain
ID: 39192551
Oh! shoot. How come I did not see that. Thank you!
0
 
LVL 28

Expert Comment

by:pepr
ID: 39193582
Beware of the explicit cast that you are using. It came from C and using it, you give up some compile-time error detection. You should prefer

    static_cast <new_type> (expression)

or
   
    reinterpret_cast <new_type> (expression)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
My project did see openJDK that I installed. What could be the problem 7 143
allswap challenge 6 99
Exception thrown at 0x00007FFD5BC81F28 7 44
Problem to picture file 20 51
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question