multimap and abstract class

B

Barry

Hi,

I have an abstract class called "Event" and a number of classes which
inherit from it, including "NoteOn" and "NoteOff".

I am now attempting to create a multimap called EventList, as follows
-

#ifndef EVENT_LIST_H

#include <map>
#include "Event.h"

class EventList : public std::multimap<double,Event>
{
public:
EventList(void);
virtual ~EventList(void);
};

#endif

#ifndef EVENT_H

class Event
{
public:
Event(void);
virtual void dummy() = 0;
virtual ~Event(void);
};

#endif

but this isn't allowed according to my compiler because 'Event' :
cannot instantiate abstract class.

I don't understand why I get this fail since I haven't even created a
EventList object yet.

What is the issue here?

Thanks,

Barry
 
B

Barry

Because the compiler evaluates the template definition, and finds out that
some of the template functions end up instantiating an abstract class,
which, of course, is an error.

 application_pgp-signature_part
< 1KVisaHämta

Thanks for the reply. I wish for my EventList class to store objects
of type NoteOn and NoteOff - not Event objects (which is abstract
anyway). But does that mean that I will have to make Event a non-
abstract class in order to create a multimap containing NoteOn and
NoteOff objects? The project of course compiles with a non abstract
Event class, but an Event is indeed an abstract concept so I'd like to
keep it abstract. Any suggestions?

Thanks,

Barry.
 
J

Juha Nieminen

Barry said:
Thanks for the reply. I wish for my EventList class to store objects
of type NoteOn and NoteOff - not Event objects (which is abstract
anyway). But does that mean that I will have to make Event a non-
abstract class in order to create a multimap containing NoteOn and
NoteOff objects? The project of course compiles with a non abstract
Event class, but an Event is indeed an abstract concept so I'd like to
keep it abstract. Any suggestions?

STL containers store objects by value, not by pointer/reference. Thus
you can't store objects of more than one type in them.

If you want to store objects of more than one type, you'll have to
make a multimap of *pointers* rather than a multimap of *objects*.
However, then the allocation and deallocation of those objects is up to
you, which will require care. (You could use smart pointers for this.)
 
J

James Kanze

I have an abstract class called "Event" and a number of
classes which inherit from it, including "NoteOn" and
"NoteOff".
I am now attempting to create a multimap called EventList, as
follows -
#ifndef EVENT_LIST_H
#include <map>
#include "Event.h"
class EventList : public std::multimap<double,Event>
{
public:
EventList(void);
virtual ~EventList(void);
};
#endif
#ifndef EVENT_H
class Event
{
public:
Event(void);
virtual void dummy() = 0;
virtual ~Event(void);
};
#endif
but this isn't allowed according to my compiler because
'Event' : cannot instantiate abstract class.
I don't understand why I get this fail since I haven't even
created a EventList object yet.
What is the issue here?

You've used std::multimap< double, Event > in a context where a
complete type definition is required, so you've instantiated the
class template. The standard says that this is undefined
behavior if you do it over a type which doesn't support a
minimum of required operations: copy construction and
assignment, for example. An abstract class doesn't fit the
bill, so the compiler can do anything it wants. Good compilers
(or library implementations) generate a compile time error.
 
J

James Kanze

Barry writes:

[...]
Because the compiler evaluates the template definition, and
finds out that some of the template functions end up
instantiating an abstract class, which, of course, is an
error.

If that were the case, it would be an error in the compiler.
The compiler is not allowed to instantiate any functions in the
context he uses the class, and may not reject the instantiation
on the basis of what some function may or may not use.

More likely, the library has implemented some form of
constraints checking, and is using that. The standard says that
the code is undefined behavior---with a naïve implementation, it
will likely pass until some function is instantiated, but an
implemenation may use concepts to catch the error earlier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top