473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
3 1565
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
11
by: Joseph Turian | last post by:
Fellow hackers, I have a class BuildNode that inherits from class Node. Similarly, I have a class BuildTree that inherits from class Tree. Tree includes a member variable: vector<Node>...
2
by: Wade | last post by:
Hi all, We have created some "Base" class pages for our WebForms and UserControls. For instance, when we create a WebForm called "WebForm1.aspx", instead of inheriting from "System.Web.UI.Page"...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
2
by: qazmlp1209 | last post by:
class base { public: base() { } base(int number) { priNumber = number ;
6
by: Doc John | last post by:
Can I create a "base" button that all the buttons in my Windows Forms can inherit from? Thanks. VS2005
3
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.