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. typecasting in std::vector type

typecasting in std::vector type

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsquestion
5 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.
  • A Offline
    A Offline
    A Ms
    wrote on last edited by
    #1

    Hi, I have using a source code representing a class hierarchical like the following:

    class c1
    {...};
    class c2 : public c1
    {...};

    now there is a method in another class which has argument like:

    void Func1(vector<c1*>& vC1);

    How could I pass the following vC2 to this method

    vector<c2*> vC2;

    Is there any way to typecast the vector<c2*> to vector<c1*>?

    E S 2 Replies Last reply
    0
    • A A Ms

      Hi, I have using a source code representing a class hierarchical like the following:

      class c1
      {...};
      class c2 : public c1
      {...};

      now there is a method in another class which has argument like:

      void Func1(vector<c1*>& vC1);

      How could I pass the following vC2 to this method

      vector<c2*> vC2;

      Is there any way to typecast the vector<c2*> to vector<c1*>?

      E Offline
      E Offline
      Eugen Podsypalnikov
      wrote on last edited by
      #2

      You could try to declare your function as:

      void Func1(vector<c1*>* pC1Vector);

      and then pass a casted pointer :-O :cool: :) :

      {
      vectror<c2*> vC2;
      ...
      Func1((vector<c1*>*) &vC2);
      }

      virtual void BeHappy() = 0;

      A 1 Reply Last reply
      0
      • E Eugen Podsypalnikov

        You could try to declare your function as:

        void Func1(vector<c1*>* pC1Vector);

        and then pass a casted pointer :-O :cool: :) :

        {
        vectror<c2*> vC2;
        ...
        Func1((vector<c1*>*) &vC2);
        }

        virtual void BeHappy() = 0;

        A Offline
        A Offline
        A Ms
        wrote on last edited by
        #3

        Thanks

        E 1 Reply Last reply
        0
        • A A Ms

          Hi, I have using a source code representing a class hierarchical like the following:

          class c1
          {...};
          class c2 : public c1
          {...};

          now there is a method in another class which has argument like:

          void Func1(vector<c1*>& vC1);

          How could I pass the following vC2 to this method

          vector<c2*> vC2;

          Is there any way to typecast the vector<c2*> to vector<c1*>?

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Short answer - no. Just because c2 is convertible to c1 doesn't mean that types dependent on c2 (like vector in this case) can be converted to equivalent types dependent on c1.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

          1 Reply Last reply
          0
          • A A Ms

            Thanks

            E Offline
            E Offline
            Eugen Podsypalnikov
            wrote on last edited by
            #5

            Maybe, it would be better (possible) to collect your c2-pointers to a vector<c1*> vC1; instead ? :)

            virtual void BeHappy() = 0;

            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