473,320 Members | 1,933 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,320 software developers and data experts.

STL containers and abstract/derived classes

I have classic interface/implementation paradigm. That does not work with
stl containers :)

Call to doAbstract(derivedList) yelds compilation error:

sample.cpp: In function 'int main(int, char**)':
sample.cpp:23: error: no matching function for call to
'doAbstract(std::list<Derived*, std::allocator<Derived*&)'
sample.cpp:12: note: candidates are: void doAbstract(const
std::list<Abstract*, std::allocator<Abstract*&)
sample.cpp:16: note: void doAbstract(const
boost::shared_ptr<Abstract>&)

For example, I've added (for me) equivalent situation with boost:shared_ptr
template that works normally.

Is this some special feature of boost:shared_ptr that std:list does not
support or I'm missing something? Is there a workaround?

#include <list>
#include <boost/shared_ptr.hpp>

class Abstract {
// whatever...
};

class Derived : public Abstract {
// whatever...
};

void doAbstract(const std::list<Abstract *&abstractList) {
// something...
}

void doAbstract(const boost::shared_ptr<Abstract&abstractPtr) {
// something...
}

int main(int argc, char *argv[]) {

std::list<Derived *derivedList;
doAbstract(derivedList); // does not work?

boost::shared_ptr<DerivedderivedPtr;
doAbstract(derivedPtr); // works...

return 0;
}

// g++ (GCC) 4.1.3 20070629 (prerelease) (Debian 4.1.2-13)
// Boost 1.33.1
Aug 16 '07 #1
1 2280
Hi!

Gordan Krešić schrieb:
void doAbstract(const std::list<Abstract *&abstractList) {
// something...
}

void doAbstract(const boost::shared_ptr<Abstract&abstractPtr) {
// something...
}

int main(int argc, char *argv[]) {

std::list<Derived *derivedList;
doAbstract(derivedList); // does not work?
No. There is not std::list constructor (which is needed here) to convert
a list<Derived*to a list<Abstract*>.
boost::shared_ptr<DerivedderivedPtr;
doAbstract(derivedPtr); // works...
Yes. shared_ptr has support for an appropriate conversion constructor.

But you can do:

void doAbstractOnce(Abstract* abstract) { ... }

std::list<Derived*list;
std::for_each(
list.begin(), list.end(),
&doAbstractOnce
);

Frank
Aug 16 '07 #2

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

Similar topics

1
by: Koen | last post by:
Hi! I have a vector containing the following: vector<Base*> theVector; theVector.push_back(new Der1()); theVector.push_back(new Der1()); theVector.push_back(new Der2());...
9
by: Christian Christmann | last post by:
Hi, I've a class Handler which contains a STL list std::list<Abstract*> mAbstract; which is storing elements of the abstract class Abstract. Further this class has a getElement function to...
6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
18
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
0
by: Craig Buchanan | last post by:
i am trying to build an application that uses plugins to extend the business functionality of an application. i've decided to use inheritance of abstract classes as the mechanism to do this. ...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.