Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-05-23
3
Medium Priority
?
608 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 800 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.
Suggested Courses

877 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