Why doesn't can't a vector of "Derived" be passed to function takingvector of "Base"?

Discussion in 'C++' started by Rob, Apr 28, 2008.

  1. Rob

    Rob Guest

    I have these classes (elided methods):

    class Base
    {
    public:
    Base(string name) {...}
    };

    class Derived : public Base
    {
    public:
    Derived(String name) : Base( name ) {...}
    };

    And neither of these work:

    /*** ATTEMPT ONE **/
    void create(std::vector<Base>& arr)
    {
    ...
    }

    int main()
    {
    std::vector<Derived> arr;
    create( arr );
    }

    /*** ATTEMPT TWO **/
    void create(std::vector<Base*>& arr)
    {
    ...
    }

    int main()
    {
    std::vector<Derived*> arr;
    create( arr );
    }
    Rob, Apr 28, 2008
    #1
    1. Advertising

  2. Re: Why doesn't can't a vector of "Derived" be passed to functiontaking vector of "Base"?

    On 2008-04-28 17:20, Rob wrote:
    > I have these classes (elided methods):



    To answer the questions in the subject (which you really ought to repeat
    in the message): Because a vector<Derived> does not inherit from
    vector<Base> even though Derived inherits from Base.

    You might want to use vector<Base*> instead and store pointers to
    objects of type Derived in it.

    --
    Erik Wikström
    Erik Wikström, Apr 28, 2008
    #2
    1. Advertising

  3. Re: Why doesn't can't a vector of "Derived" be passed to function taking vector of "Base"?

    Rob <> writes:

    > I have these classes (elided methods):
    >
    > class Base
    > {
    > public:
    > Base(string name) {...}
    > };
    >
    > class Derived : public Base
    > {
    > public:
    > Derived(String name) : Base( name ) {...}
    > };
    >
    > And neither of these work:
    >
    > /*** ATTEMPT ONE **/
    > void create(std::vector<Base>& arr)
    > {
    > ...
    > }
    >
    > int main()
    > {
    > std::vector<Derived> arr;
    > create( arr );
    > }


    This should be obvious why.


    > /*** ATTEMPT TWO **/
    > void create(std::vector<Base*>& arr)


    You are telling the C++ compiler that create will put pointers to
    instances of Base, or any subclass of Base in arr. That means
    instances that are totally unrelated to Derived.


    > int main()
    > {
    > std::vector<Derived*> arr;
    > create( arr );
    > }


    Here, you're telling the C++ compiler that arr will contain only
    instances of Derived, or subclasses. But how can the C++ compiler
    know that create will effectively put in arr instances of Derived, and
    not instances of Base, or instances of SomethingElse that is a
    subclass of Base? Worse, this is not something that can be determined
    at compilation time, in general. There are theorems proving that it
    is impossible.


    Once again, what you're wanting is a more dynamic programming language
    (may I suggest humbly: Common Lisp).


    That said, the stl is not the Omega of the C++ libraries. You can
    program in C++ in a very different style, using different libraries,
    like Lpp: http://www.interhack.net/projects/lpp/ or even Boost::Any.

    These libraries would allow you to specify a more general type of
    container for the argument of create, and let you prove by yourself
    that you indeed only put instances of Derived in that vector.

    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Apr 28, 2008
    #3
  4. Rob

    Daniel Kraft Guest

    Re: Why doesn't can't a vector of "Derived" be passed to functiontaking vector of "Base"?

    Rob wrote:
    > I have these classes (elided methods):
    >
    > class Base
    > {
    > public:
    > Base(string name) {...}
    > };
    >
    > class Derived : public Base
    > {
    > public:
    > Derived(String name) : Base( name ) {...}
    > };
    >
    > And neither of these work:
    >
    > /*** ATTEMPT ONE **/
    > void create(std::vector<Base>& arr)
    > {
    > ...
    > }
    >
    > int main()
    > {
    > std::vector<Derived> arr;
    > create( arr );
    > }


    I don't know what create() should do, but if it does *create* objects of
    type Base and store it into the vector, how can you be sure it doesn't
    create objects of type Foo : Base and end up having those in your vector
    of Derived objects?

    So this is probably why std::vector<Base> and std::vector<Derived> are
    completely different.

    > /*** ATTEMPT TWO **/
    > void create(std::vector<Base*>& arr)
    > {
    > ...
    > }
    >
    > int main()
    > {
    > std::vector<Derived*> arr;
    > create( arr );
    > }


    And the same here.

    Daniel


    --
    Done: Bar-Sam-Val-Wiz, Dwa-Elf-Hum-Orc, Cha-Law, Fem-Mal
    Underway: Ran-Gno-Neu-Fem
    To go: Arc-Cav-Hea-Kni-Mon-Pri-Rog-Tou
    Daniel Kraft, Apr 28, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CD
    Replies:
    2
    Views:
    797
    Victor Bazarov
    Oct 5, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,764
    Smokey Grindel
    Dec 2, 2006
  3. call_me_anything

    vector <Base *> = vector <Derived *> ??

    call_me_anything, Jan 23, 2007, in forum: C++
    Replies:
    16
    Views:
    608
    Noah Roberts
    Jan 24, 2007
  4. Replies:
    1
    Views:
    387
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    374
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page