Solved

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

Posted on 2013-05-23
3
579 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now