Solved

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

Posted on 2013-05-23
3
601 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 29

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

623 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