Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using dynamic_cast with template classes.

using dynamic_cast with template classes.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

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

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

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

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

    bp = dynamic\_cast\*> (&sqrInt\_ob);          //sqrInt\_ob is from a derived class of Num.
    if(bp) {
        cout << "Cast from sqrNum\* to Num\* OK.\\n"
             << "Value is " << bp->getval() << endl;
    }
    else
        cout << "Error !\\n\\n";
    
    dp = dynamic\_cast\> (&numInt\_ob);        //numInt\_ob is base class object and SqrNum is a derived class.
    if(dp)
        cout << "Error !\\n";
    else {
        cout << "Cast from Num\* to SqrNum\* 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\*> (&numDouble\_ob);        //Num of type int & numDouble\_ob is of type Num
    if(bp)
        cout << "Error" << endl;
    else {
        cout << "Can't cast from Num\* to Num\*.\\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

    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
    using namespace std;

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

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

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

    bp = dynamic\_cast\*> (
    
    L C 2 Replies Last reply
    0
    • T Tarun Jha

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

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

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

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

      bp = dynamic\_cast\*> (&sqrInt\_ob);          //sqrInt\_ob is from a derived class of Num.
      if(bp) {
          cout << "Cast from sqrNum\* to Num\* OK.\\n"
               << "Value is " << bp->getval() << endl;
      }
      else
          cout << "Error !\\n\\n";
      
      dp = dynamic\_cast\> (&numInt\_ob);        //numInt\_ob is base class object and SqrNum is a derived class.
      if(dp)
          cout << "Error !\\n";
      else {
          cout << "Cast from Num\* to SqrNum\* 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\*> (&numDouble\_ob);        //Num of type int & numDouble\_ob is of type Num
      if(bp)
          cout << "Error" << endl;
      else {
          cout << "Can't cast from Num\* to Num\*.\\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

      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
      using namespace std;

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

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

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

      bp = dynamic\_cast\*> (
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      dp = dynamic_cast*> (&numInt_ob); //numInt_ob is base class object and SqrNum is a

      Needs an '*' after <int>. Code now runs successfully.

      T 1 Reply Last reply
      0
      • L Lost User

        dp = dynamic_cast*> (&numInt_ob); //numInt_ob is base class object and SqrNum is a

        Needs an '*' after <int>. Code now runs successfully.

        T Offline
        T Offline
        Tarun Jha
        wrote on last edited by
        #3

        i added * after SqrNum but it is still giving that error .

        L 1 Reply Last reply
        0
        • T Tarun Jha

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

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

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

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

          bp = dynamic\_cast\*> (&sqrInt\_ob);          //sqrInt\_ob is from a derived class of Num.
          if(bp) {
              cout << "Cast from sqrNum\* to Num\* OK.\\n"
                   << "Value is " << bp->getval() << endl;
          }
          else
              cout << "Error !\\n\\n";
          
          dp = dynamic\_cast\> (&numInt\_ob);        //numInt\_ob is base class object and SqrNum is a derived class.
          if(dp)
              cout << "Error !\\n";
          else {
              cout << "Cast from Num\* to SqrNum\* 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\*> (&numDouble\_ob);        //Num of type int & numDouble\_ob is of type Num
          if(bp)
              cout << "Error" << endl;
          else {
              cout << "Can't cast from Num\* to Num\*.\\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

          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
          using namespace std;

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

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

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

          bp = dynamic\_cast\*> (
          
          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Quote:

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

          Should be

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

          T 1 Reply Last reply
          0
          • C CPallini

            Quote:

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

            Should be

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

            T Offline
            T Offline
            Tarun Jha
            wrote on last edited by
            #5

            Quote:

            return (Num::val * Num::val);

            but i have already inherited all the members of the base class to the derived class, then why i need to use scope resolution operator to point where to look for val ?

            C 1 Reply Last reply
            0
            • T Tarun Jha

              i added * after SqrNum but it is still giving that error .

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Did not give that error in the original code that I compiled.

              1 Reply Last reply
              0
              • T Tarun Jha

                Quote:

                return (Num::val * Num::val);

                but i have already inherited all the members of the base class to the derived class, then why i need to use scope resolution operator to point where to look for val ?

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                See [35.19] Why am I getting errors when my template-derived-class uses a member it inherits from its template-base-class?[^]

                T 1 Reply Last reply
                0
                • C CPallini

                  See [35.19] Why am I getting errors when my template-derived-class uses a member it inherits from its template-base-class?[^]

                  T Offline
                  T Offline
                  Tarun Jha
                  wrote on last edited by
                  #8

                  thank's

                  C 1 Reply Last reply
                  0
                  • T Tarun Jha

                    thank's

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    You are welcome.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups