Solved

templates and abstract classes

Posted on 1998-03-14
11
231 Views
Last Modified: 2010-04-10
Help!

I am trying to input into a list, objects from an abstract class top_class...

when I call the list constructor
 sortablelist<top_class> L;
I get an error
"cannot create instance of abstract class"  I understand that this is due to the fact that the compiler cannot resolve for the methods (which are only defined in the sub-classes) of this abstract class...

My problem is that I don't really know how to fix this..

Do I need to send a pointer to the object in order to allow polymorphism?

I don't know how since
 sortablelist<top_class*> L;
produces an error... can't find a match for...(above line constructor)
How can I fix this (please don't just say implement this in the sortable list!...I need to know how!), any code would be very helpful.... also if you need the implementation of sortablelist just let me know.

here's what my class looks like

class top_class{
  protected:
         int depth;
         int order;
       
  public:
      top_class(int);            //constructor
        virtual void M1(C&)=0;      //abstract method
        int getdepth();
        int getorder();
};


I need to place these items in the list and also be able to use the getdepth() method from an iterator (this I have)


thanks in advance
0
Comment
Question by:rafy
11 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1183472
If I'm not mistaken, you problem arrises from the fact that an abstract class does not actualy exist in memory and cannot be instantiated.

An abstract class is only a definition of an an object, not a declaration, and as such will not allow either a pointer or a referance to it.

In order to fix this, you will need to derive an object from your abstract class and define all the virtual methods. The definition of these methods needs not do anything usefull, say:

void derived_top_Class:M1(int /*or whatebver*/ C&){
   return;
}

In this fashion, you insure that you are using a declarable object that can be instantiated.
0
 
LVL 32

Expert Comment

by:jhance
ID: 1183473
An instance of an abstract class can never be created.  You must derive a class of your own from the abstract class and you MUST overide each and every abstract method.  So your top_class might be used as follows:

class my_class : top_class
{
   virtual void M1(C&);  // MUST HAVE AN IMPLEMENTATION!
};


Now you can add an instance of your my_class in your program.


0
 
LVL 22

Expert Comment

by:nietod
ID: 1183474
jhance is correct, but I feel a little bit unclear.  And abstract class is a class that has one ore more functios that are "pure virtual"  that is, that are defined using the "= 0" syntax as below.

class abstract
{
 virtual void SomeFunction() = 0;
};

A concretec class has no pure virtual functions.  When you want to derive a concrete class from an abstract class, you must redefine each of the pure abstract functions.  Like

class Concrete : public Abstract
{
 virtual void SomeFunction() { cout << "I'm concrete"; };
};

Note also that it is possible to derive an abstract class from an abstract class (Either by not defining some of the pure virtual functions, or be difining new ones)  Also it is possible to derive an abstract class from a concrete class, by adding pure virtual functions.

0
 

Author Comment

by:rafy
ID: 1183475
Maybe I wasn't clear...

I declared the method virtual because I DO have it implemented in sub-classes of top class.  The problem is that I have two different subclasses (close to similar but not exact copies), and the sortable list is supposed to store instances from both these subclasses and sort them by the "order" variable which is present in both!

ie this exist

class top_class{
  protected:
         int depth;
         int order;
       
  public:
top_class(int); //constructor
        virtual void M1(C&)=0; //abstract method
        int getdepth();
        int getorder();
};

class der1: public top_class{
     protected:
        some instance vars of this subclass;

      public:
         void M1(C&);   //this IS implemented
};
class der2: public top_class{
     protected:
        instance vars of this subclass;

      public:
         void M1(C&);    //and this IS implemented
};
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183476
The problem must be the "sortable list", which you haven't show us.  It must be trying to store the base type (top_class) which it can't because it is abstract.  However it can store pointers to the base class,  (top_class *)
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 22

Expert Comment

by:nietod
ID: 1183477
if the list is a template type, like list<>, it should be declated as

list<top_class *> SortList;

not as

list<top_class> SortList;

If that doesn't help, post the code for the list.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183478
Looking back, maybe more of an explantation is in order.  The list (almost certainly) cannot store objects of the two derived classes directly.  Few containers can do this because the objects may have different sizes and they do have different types which means that C++'s type checking would have to be circumvented.  Now there are some exceptions to this, but usually if you need to store more than one type of object in a container (list, array, hash table, etc) what you do is to create the objects dynamically using new, and then store pointers to the base class in the container.  This works well since pointers to the derived classes will automatically be converted to pointers to the base classes.  For example.

SortList.Add(new der1);  // add pointer to a der1;
SortList.Add(new der2);  // add pointer to a der2;

Now you have to make sure that the objects in the list are eventually destroyed with delete.  
0
 

Author Comment

by:rafy
ID: 1183479
my mistake was with the template declaration...thanks

but how do I give the points to nietod (not jhance??)
0
 

Author Comment

by:rafy
ID: 1183480
Please let nietod answer this question so that I can give him his points...thanks!
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1183481
You're welcome.  the problem was that you were not storig pointers then?
0
 

Author Comment

by:rafy
ID: 1183482
well, sort of...

I had just forgotten to change the template to the pointer notation (I was only writing the constructor with the *...

stipid eh?

Anyways thanks...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now