By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

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

P: n/a
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<Derivedarr;
create( arr );
}

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

int main()
{
std::vector<Derived*arr;
create( arr );
}
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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<Derivedarr;
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<Baseand std::vector<Derivedare
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
Jun 27 '08 #2

P: n/a
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<Deriveddoes not inherit from
vector<Baseeven 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
Jun 27 '08 #3

P: n/a
Rob <so***************@yahoo.comwrites:
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<Derivedarr;
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__
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.