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

R

Rob

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 );
}
 
E

Erik Wikström

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.
 
P

Pascal J. Bourguignon

Rob said:
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.
 
D

Daniel Kraft

Rob said:
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?

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

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

And the same here.

Daniel
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top