473,724 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheriting from std::vector?

BCC
Hi,

A colleague has some code like this:

class CMyObject {
// Bunch of Member functions
}

class CMyObjectList: public std::vector<CMy Object>
{
// Bunch of member functions
}

I need to inherit the functionality of his object and list as well as add
some of my own:

class CMyPersonalObje ct: public CMyObject
{
// More stuff
}

class CMyPersonalObje ctList: public CMyObjectList
{
// More stuff
}

But if I pass in CMyPersonalObje ctList to a function, and then try to assign
the CMyPersonalObje ct to a local variable of that type, I get a compiler
error telling me that I cannot convert CMyObject to CMyPersonalObje ct... the
compiler thinks the list I passed in is made of the parent objects.

What is going on and how do I fix it?

Thanks,
B
Jul 19 '05 #1
26 10725
BCC wrote:

But if I pass in CMyPersonalObje ctList to a function, and then try to assign
the CMyPersonalObje ct to a local variable of that type, I get a compiler
error telling me that I cannot convert CMyObject to CMyPersonalObje ct... the
compiler thinks the list I passed in is made of the parent objects.

What is going on and how do I fix it?


Would need to see the code where the assignment is made.
However, as std::vector doesn't have a virtual destructor it
shouldn't really be being used as a base class. That isn't
your actual problem though.

Jul 19 '05 #2
On Thu, 23 Oct 2003 23:47:14 GMT, "BCC" <br***@akanta.c om> wrote:
A colleague has some code like this:

class CMyObject {
// Bunch of Member functions
}
Missing semicolon.
class CMyObjectList: public std::vector<CMy Object>
{
// Bunch of member functions
}
Ditto.

Without further information this inheritance is a little suspicious.

I can thing of good reasons to inherit from std::string, but not std::vector.

I need to inherit the functionality of his object and list as well as add
some of my own:

class CMyPersonalObje ct: public CMyObject
{
// More stuff
}

class CMyPersonalObje ctList: public CMyObjectList
{
// More stuff
}

But if I pass in CMyPersonalObje ctList to a function, and then try to assign
the CMyPersonalObje ct to a local variable of that type,
There is no relationship between CMyPersonalObje ctList and CMyPersonalObje ct
in what you have shown, so the above seems meaningless.
I get a compiler
error telling me that I cannot convert CMyObject to CMyPersonalObje ct...
The compiler is right, you cannot do that unless you provide a conversion.
the compiler thinks the list I passed in is made of the parent objects.
Where on earth did you get that absurd idea? Compilers don't think.
What is going on
See e.g. <url: http://news.google.com/>.
and how do I fix it?


Perhaps follow the advice of Michael Moore?

Jul 19 '05 #3
On Thu, 23 Oct 2003 23:47:14 GMT, BCC <br***@akanta.c om> wrote:
Hi,

A colleague has some code like this:

class CMyObject {
// Bunch of Member functions
}

class CMyObjectList: public std::vector<CMy Object>
{
// Bunch of member functions
}

I need to inherit the functionality of his object and list as well as add
some of my own:

class CMyPersonalObje ct: public CMyObject
{
// More stuff
}

class CMyPersonalObje ctList: public CMyObjectList
{
// More stuff
}

But if I pass in CMyPersonalObje ctList to a function, and then try to
assign
the CMyPersonalObje ct to a local variable of that type, I get a compiler
error telling me that I cannot convert CMyObject to CMyPersonalObje ct...
the
compiler thinks the list I passed in is made of the parent objects.

What is going on and how do I fix it?

Thanks,
B


std::vector is a template . CMyObjectList should also be a template, at
least if you want to have personalobject list.

template <class T> CObjectList : public std::vector<T> {
some more
};

typedef CObjectList<CMy PersonalObject> CMyPersonalObje ctList;
--
grzegorz
Jul 19 '05 #4
"Alf P. Steinbach" <al***@start.no > wrote in message
news:3f******** ********@News.C IS.DFN.DE...
the compiler thinks the list I passed in is made of the parent objects.


Where on earth did you get that absurd idea? Compilers don't think.


Understatement of the year. :-)

-Mike

Jul 19 '05 #5

"BCC" <br***@akanta.c om> wrote in message
news:6M******** ************@ne wssvr21.news.pr odigy.com...
Hi,

A colleague has some code like this:

class CMyObject {
// Bunch of Member functions
}

class CMyObjectList: public std::vector<CMy Object>
{
// Bunch of member functions
}

I need to inherit the functionality of his object and list as well as add
some of my own:

class CMyPersonalObje ct: public CMyObject
{
// More stuff
}

class CMyPersonalObje ctList: public CMyObjectList
{
// More stuff
}

But if I pass in CMyPersonalObje ctList to a function, and then try to assign the CMyPersonalObje ct to a local variable of that type, I get a compiler
error telling me that I cannot convert CMyObject to CMyPersonalObje ct... the compiler thinks the list I passed in is made of the parent objects.
It won't work, because the vector contains CMyObjects, not
CMyPersonalObje cts. So whenever you insert a CMyPersonalObje ct in that
vector, it'll get "truncated" to it's base type. You might be able to use a
vector of pointers to (dynamically-allocated) CMyObjects (+derivatives)
instead, but you'll run into other problems:
- Manual memory allocation is difficult and error-prone (but "smart pointer"
libraries might help, see for example www.boost.org )
- You'll have to downcast the stored addresses all the time
- I think it is wrong to have CMyPersonalObje ctList as a subclass of
CMyObjectList, because such hierarchy permits insertions of CMyObjects into
CMyPersonalObje ctList (you might want to do a search on "Liskov Substitution
Principle" to learn why this is a bad idea. For example read this article:
http://www.brent.worden.org/articles...rinciple.html).

What exactly are you trying to do? Maybe there's a better way to do it.

What is going on and how do I fix it?

Thanks,
B

Jul 19 '05 #6
Mike Wahler wrote:
"Alf P. Steinbach" <al***@start.no > wrote in message
news:3f******** ********@News.C IS.DFN.DE...
the compiler thinks the list I passed in is made of the parent objects.


Where on earth did you get that absurd idea? Compilers don't think.

Understatement of the year. :-)


OP must have confused "The Compiler That Thinks" with "The Thinking
Man's Compiler."

/david :-)

--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."

Jul 19 '05 #7
lilburne wrote:
However, as std::vector doesn't have a virtual destructor it
shouldn't really be being used as a base class.


There is no problem with using it as a base class as long as you don't
destroy the object through a pointer to std::vector.

Jul 19 '05 #8
Rolf Magnus wrote:
lilburne wrote:

However, as std::vector doesn't have a virtual destructor it
shouldn't really be being used as a base class.

There is no problem with using it as a base class as long as you don't
destroy the object through a pointer to std::vector.


And you ensure that how?

Jul 19 '05 #9
lilburne wrote:

Rolf Magnus wrote:
lilburne wrote:

However, as std::vector doesn't have a virtual destructor it
shouldn't really be being used as a base class.

There is no problem with using it as a base class as long as you don't
destroy the object through a pointer to std::vector.


And you ensure that how?


You don't do it.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
5961
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
18
2868
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
20
17828
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
17
3352
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
8
5109
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
32
69682
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: std::vector<int> fun(){ //build the vector v; return v; }
56
5794
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
9
8896
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter; Any ideas why is tihs happening? The code is as follows: template <class T> struct StdVectorStorage { std::vector<T>* _storage;
13
2957
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
0
8868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8741
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9243
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9160
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9090
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8063
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6689
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4771
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2613
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.