Click here to Skip to main content
15,887,175 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Advice on designing a simple task scheduler in embedded C, that can handle asynchronuous tasks? Pin
leon de boer11-Jun-18 2:52
leon de boer11-Jun-18 2:52 
GeneralRe: Advice on designing a simple task scheduler in embedded C, that can handle asynchronuous tasks? Pin
arnold_w11-Jun-18 3:13
arnold_w11-Jun-18 3:13 
GeneralRe: Advice on designing a simple task scheduler in embedded C, that can handle asynchronuous tasks? Pin
leon de boer11-Jun-18 4:04
leon de boer11-Jun-18 4:04 
GeneralRe: Advice on designing a simple task scheduler in embedded C, that can handle asynchronuous tasks? Pin
arnold_w11-Jun-18 9:38
arnold_w11-Jun-18 9:38 
AnswerRe: Advice on designing a simple task scheduler in embedded C, that can handle asynchronuous tasks? Pin
supercat920-Jun-18 12:22
supercat920-Jun-18 12:22 
QuestionC# combo box text color change Pin
czaar9998-Jun-18 7:18
czaar9998-Jun-18 7:18 
AnswerRe: C# combo box text color change Pin
Richard Andrew x648-Jun-18 10:54
professionalRichard Andrew x648-Jun-18 10:54 
Questionusing dynamic_cast with template classes. Pin
Tarun Jha5-Jun-18 1:48
Tarun Jha5-Jun-18 1:48 
//  Demonstrate dynamic_cast on template classes.
#include <iostream>
using namespace std;

template <class T>
class Num {
protected:
    T val;
public:
    Num(T x) { val = x;}
    virtual T getval() {    return val;    }
};

template <class T>
class SqrNum : public Num<T> {
public:
    SqrNum(T x) : Num<T>(x) {}
    T getval()  {   return (val * val);   }
};

int main(){
    Num<int> *bp, numInt_ob(2);
    SqrNum<int> *dp, sqrInt_ob(3);
    Num<double> numDouble_ob(3.3);

    bp = dynamic_cast<Num<int>*> (&sqrInt_ob);          //sqrInt_ob is from a derived class of Num<int>.
    if(bp) {
        cout << "Cast from sqrNum<int>* to Num<int>* OK.\n"
             << "Value is " << bp->getval() << endl;
    }
    else
        cout << "Error !\n\n";

    dp = dynamic_cast<SqrNum<int>> (&numInt_ob);        //numInt_ob is base class object and SqrNum is a derived class.
    if(dp)
        cout << "Error !\n";
    else {
        cout << "Cast from Num<int>* to SqrNum<int>* not OK.\n"
             << "Can't cast a pointer to a base object into\n"
             << "a pointer to a derived object\n\n";
    }

    bp = dynamic_cast<Num<int>*> (&numDouble_ob);        //Num<int> of type int & numDouble_ob is of type Num<double>
    if(bp)
        cout << "Error" << endl;
    else {
        cout << "Can't cast from Num<double>* to Num<int>*.\n"
             << "These are 2 different types.\n\n";
    }

    return 0;
}


in the code above i m getting error that
Quote:
error: 'val' was not declared in this scope
T getval() { return (val val); }
^~~


but
Quote:
class SqrNum : public Num<t>

according to this the protected value in the base class should just work fine as protected value in the derived class, but it isn't ?
How do i solve this problem ?

EDIT :
below is the working code:

//  Demonstrate dynamic_cast on template classes.
#include <iostream>
using namespace std;

template <class T>
class Num {
protected:
    T val;
public:
    Num(T x) { val = x;}
    virtual T getval() {    return val;    }
};

template <class T>
class SqrNum : public Num<T> {
public:
    SqrNum(T x) : Num<T>(x) {}
    T getval()  {   return (Num<T>::val * Num<T>::val);   }
};

int main(){
    Num<int> *bp, numInt_ob(2);
    SqrNum<int> *dp, sqrInt_ob(3);
    Num<double> numDouble_ob(3.3);

    bp = dynamic_cast<Num<int>*> (&sqrInt_ob);          //sqrInt_ob is from a derived class of Num<int>.
    if(bp) {
        cout << "Cast from sqrNum<int>* to Num<int>* OK.\n"
             << "Value is " << bp->getval() << endl;
    }
    else
        cout << "Error !\n\n";

    dp = dynamic_cast<SqrNum<int>*> (&numInt_ob);        //numInt_ob is base class object and SqrNum is a derived class.
    if(dp)
        cout << "Error !\n";
    else {
        cout << "Cast from Num<int>* to SqrNum<int>* not OK.\n"
             << "Can't cast a pointer to a base object into\n"
             << "a pointer to a derived object\n\n";
    }

    bp = dynamic_cast<Num<int>*> (&numDouble_ob);        //Num<int> of type int & numDouble_ob is of type Num<double>
    if(bp)
        cout << "Error" << endl;
    else {
        cout << "Can't cast from Num<double>* to Num<int>*.\n"
             << "These are 2 different types.\n\n";
    }

    return 0;
}

Thank you

modified 6-Jun-18 0:49am.

AnswerRe: using dynamic_cast with template classes. Pin
Richard MacCutchan5-Jun-18 3:20
mveRichard MacCutchan5-Jun-18 3:20 
GeneralRe: using dynamic_cast with template classes. Pin
Tarun Jha5-Jun-18 8:31
Tarun Jha5-Jun-18 8:31 
GeneralRe: using dynamic_cast with template classes. Pin
Richard MacCutchan5-Jun-18 21:00
mveRichard MacCutchan5-Jun-18 21:00 
AnswerRe: using dynamic_cast with template classes. Pin
CPallini5-Jun-18 11:03
mveCPallini5-Jun-18 11:03 
GeneralRe: using dynamic_cast with template classes. Pin
Tarun Jha5-Jun-18 18:45
Tarun Jha5-Jun-18 18:45 
GeneralRe: using dynamic_cast with template classes. Pin
CPallini5-Jun-18 21:22
mveCPallini5-Jun-18 21:22 
GeneralRe: using dynamic_cast with template classes. Pin
Tarun Jha6-Jun-18 3:11
Tarun Jha6-Jun-18 3:11 
GeneralRe: using dynamic_cast with template classes. Pin
CPallini6-Jun-18 22:26
mveCPallini6-Jun-18 22:26 
QuestionDevice manager in windows. Pin
Member 138414544-Jun-18 9:14
Member 138414544-Jun-18 9:14 
SuggestionRe: Device manager in windows. Pin
David Crow4-Jun-18 10:09
David Crow4-Jun-18 10:09 
GeneralTiniest portable executable Pin
DaviFN4-Jun-18 6:25
DaviFN4-Jun-18 6:25 
GeneralRe: Tiniest portable executable Pin
leon de boer4-Jun-18 20:17
leon de boer4-Jun-18 20:17 
Questioni would like to be a programmer Pin
Member 1384988629-May-18 7:58
Member 1384988629-May-18 7:58 
SuggestionRe: i would like to be a programmer Pin
David Crow29-May-18 8:31
David Crow29-May-18 8:31 
AnswerRe: i would like to be a programmer Pin
Victor Nijegorodov29-May-18 9:27
Victor Nijegorodov29-May-18 9:27 
AnswerRe: i would like to be a programmer Pin
CPallini29-May-18 23:01
mveCPallini29-May-18 23:01 
AnswerRe: i would like to be a programmer Pin
jfbode102930-May-18 4:33
jfbode102930-May-18 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.