0

I need to add a few methods to c++'s class. I'm creating a new class using inheritance called "Super_list" that will inherit all of list's methods and allow my to add my own.

#ifndef SUPER_LIST_H
#define SUPER_LIST_H

#include "my_containter.h"
#include <list>
using namespace std;

class My_Container;

class Super_list: public list<My_Container>
{
 public:
  void new_func1();
  void new_func2();
  void new_func_3();
};
#endif

This is where I'm using my newly made class:

#ifndef my_container_H
#define my_container_H

#include <list> 
#include "super_list.h"
using namespace std;

class Super_list;

class My_container
{
 private:
  Super_list buddy;
};
#endif

I'm getting a bunch of error relating to the inheritance not being done correctly. I would appreciate any help or other ideas from completing this task.

Thanks :)

J.Bet-Eivazi
  • 117
  • 3
  • 7
  • What are the error messages? Also, what kinds of objects are stored in the "super_list"? – Code-Apprentice Nov 29 '12 at 20:13
  • 6
    Standard library containers are not designed to be inherited from as they do not have virtual destructors. I would suggest either using a class with a list member or making the functions you need free functions that take a list parameter. – David Brown Nov 29 '12 at 20:16

3 Answers3

2

You have a cyclic dependency: MyContainer needs so know about Super_list and vice versa. You need to find a way to break this dependency. Note that in your code, the forward declarations are completely superfluous.

Note also that standard library containers aren't designed to be inherited from publicly.

Concerning the dependency, you need to modify at least one of your classes such that it does not need the full definition of the other. Pretending for a moment that publicly inheriting from std::list is OK, then the only option would be for My_Container not to need the full definition of SuperList by having it hold a (preferably smart) pointer:

class My_container
{
 private:
  Super_list* buddy;
}; 

This would allow you to remove the super_list.h include.

One unrelated warning: it is not good to put using namespace std in header files, since it will impose that using directive on all code that, directly or indirectly, includes your header. This can lead to all kind of trouble. I usually go farther and say you shouldn't use it anywhere. Not everyone agrees with me on that one.

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • "...find a way to break dependancy". What can be done besides forwards declarations? – TeaOverflow Nov 29 '12 at 20:22
  • 1
    @Evgeni the forward declaration does absolutely nothing in the code. The header is included, and the code wouldn't compile without it either. The design must be changed considerably, and how to do it depends on OP's aims. – juanchopanza Nov 29 '12 at 20:24
  • In my case it always seemed to break the cycle. It always arose when "registering" the enclosing class with its member. – TeaOverflow Nov 29 '12 at 20:28
  • @Evgeni with this code, you cannot forward declare the classes. Each one needs the full declaration of the other. – juanchopanza Nov 29 '12 at 20:29
  • Thanks, I see it now as well. But why is `list` "not ok"? As long as I remember correctly its `list` with `T*` as member? – TeaOverflow Nov 29 '12 at 20:39
  • @Evgeni `std::list` holds `T` objects and needs the full declaration. `std::list` holds pointers to `T` and doesn't need it. – juanchopanza Nov 29 '12 at 20:41
0

Can't say much without the error messeages.

1) You probably want to define some constructors in Super_list which forward their arguments to the std::list constructors.

2) Every time I tried to do something like this (or worked with something like this) it turned out to be a BAD idea. To keep incapsulation, what you probably want is some global functions:

template<class T>
new_func1(std::list<T> &l)

template<class T>
new_func2(std::list<T> &l)

etc.

sbabbi
  • 11,070
  • 2
  • 29
  • 57
0

I would ask if you really do need to inherit to gain additional functionality. Try using a list as a member of your class. Not only will it make your class easier to change, but it means that code that uses your class won't care about whether its a list or not.

Here is more information

Community
  • 1
  • 1
Carl
  • 43,122
  • 10
  • 80
  • 104