Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why doesn't can't a vector of "Derived" be passed to function takingvector of "Base"?

Reply
Thread Tools

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

 
 
Rob
Guest
Posts: n/a
 
      04-28-2008
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 );
}
 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      04-28-2008
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
 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      04-28-2008
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__
 
Reply With Quote
 
Daniel Kraft
Guest
Posts: n/a
 
      04-28-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to implement this function so that the vector can be passed backto the calling function? xz C++ 2 03-14-2008 11:02 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57