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

A problem in defining a common interface

hi everyone!
I have a problem in implementing a common class interface.
my assignment is to implement a data structure list, and I have define
a class template list_base, it's an abstract class, only define the
common interface of all various list. The definition is below:

template <typename T>
class list_base
{
public:
struct list_node;

virtual ~list_base() = 0;
virtual void push_back(const T&) = 0;
virtual void push_front(const T&) = 0;
virtual void insert(const list_node *node, const T&) = 0;
virtual void earse(list_node *node) = 0;
virtual void pop_back() = 0;
virtual void pop_front() = 0;
virtual T back() const = 0;
virtual T front() const = 0;
virtual list_node* search(const T&) const = 0;
virtual size_t size() const = 0;
};

then I implement a circular list just like this

template <typename T>
class circular_list : public list_base<T>
{
public:

circular_list();
~circular_list();

virtual void push_back(const T&);
virtual void push_front(const T&);
virtual void insert(const list_node *node, const T&);
virtual void earse(list_node *node);
virtual void pop_back();
virtual void pop_front();
virtual T back() const;
virtual T front() const;
virtual list_node* search(const T&) const;
virtual size_t size() const;
private:
circular_list(const circular_list &);
circular_list & operator = (const circular_list &);
list_node *head;
size_t size_;
};

how could I implement the list node structure?
because different list type need different list node type, e.g.
single-linked list is just want a next pointer, but a double-linked
list want a prev pointer besides a next.
define another list_node_base??
thank you for answering it for me, and forgiving my poor English.

Jan 27 '07 #1
6 1893
On 26 Jan 2007 21:21:40 -0800, "Wayne Shu" <Wa******@gmail.comwrote:
>how could I implement the list node structure?
Have you considered having each concrete list type implement its own node
structure? e.g.

template <typename T>
class list_base
{
public:
virtual ~list_base() = 0;
virtual void push_back(const T&) = 0;
virtual void push_front(const T&) = 0;
virtual void insert(const list_node *node, const T&) = 0;
virtual void earse(list_node *node) = 0;
virtual void pop_back() = 0;
virtual void pop_front() = 0;
virtual T back() const = 0;
virtual T front() const = 0;
virtual list_node* search(const T&) const = 0;
virtual size_t size() const = 0;
};

template <typename T>
class single_link_list : public list_base<T>
{
public:
/* ... */

private:
struct node
{
T* data;
node* next;
};
node* head;
};

template <typename T>
class double_link_list : public list_base<T>
{
public:
/* ... */

private:
struct node
{
T* data;
node* prev;
node* next;
};
node* head;
};

Jan 27 '07 #2


On Jan 27, 3:29 pm, Dave Rahardja <a...@me.comwrote:
On 26 Jan 2007 21:21:40 -0800, "Wayne Shu" <Wayne...@gmail.comwrote:
how could I implement the list node structure?Have you considered having each concrete list type implement its own node
structure? e.g.
the code below can't be compiled, because the list_node in the base
class is an unknown type,
but if you add a forward declaration of list_node, you can't compile it
either.
template <typename T>
class list_base
{
public:
virtual ~list_base() = 0;
virtual void push_back(const T&) = 0;
virtual void push_front(const T&) = 0;
virtual void insert(const list_node *node, const T&) = 0;
virtual void earse(list_node *node) = 0;
virtual void pop_back() = 0;
virtual void pop_front() = 0;
virtual T back() const = 0;
virtual T front() const = 0;
virtual list_node* search(const T&) const = 0;
virtual size_t size() const = 0;

};template <typename T>
class single_link_list : public list_base<T>
{
public:
/* ... */

private:
struct node
{
T* data;
node* next;
};
node* head;

};template <typename T>
class double_link_list : public list_base<T>
{
public:
/* ... */

private:
struct node
{
T* data;
node* prev;
node* next;
};
node* head;

};
Jan 27 '07 #3
Wayne Shu wrote:
I have a problem in implementing a common class interface.

template <typename T>
class list_base
{
public:
struct list_node;

virtual ~list_base() = 0;
virtual void push_back(const T&) = 0;
virtual void push_front(const T&) = 0;
virtual void insert(const list_node *node, const T&) = 0;
virtual void earse(list_node *node) = 0;
virtual void pop_back() = 0;
virtual void pop_front() = 0;
virtual T back() const = 0;
virtual T front() const = 0;
virtual list_node* search(const T&) const = 0;
virtual size_t size() const = 0;
};
With the pure virual dtor
virtual ~list_base() = 0;
ovkect of the class can not be linked, so i think it is easyer to define
dtor at the point of declaration

virtual ~list_base() {}

What means "list_node*" for list users?
virtual void insert(const list_node *node, const T&) = 0;
virtual void earse(list_node *node) = 0;
virtual list_node* search(const T&) const = 0;
If you make interface of "common list" you can not declare derived-specific
parts, as "list_node". So, you need declare common interface of "list_node"
also.

Why do you use pointer to "list_node"? If "list_node" stored in your
"concrete list", "concrete list" must create and destroy "list_node", so it
is better to use references instead of pointers - "list_node&"

class list_node
{
public:
virtual method1()=0;
virtual method2()=0;

virtual ~list_node(){}
};

template <typename T>
class list_base
{
public:
virtual ~list_base(){}
virtual void push_back(const T&) = 0;
virtual void push_front(const T&) = 0;
virtual void insert(const list_node& node, const T&) = 0;
virtual void earse(list_node& node) = 0;
virtual void pop_back() = 0;
virtual void pop_front() = 0;
virtual T back() const = 0;
virtual T front() const = 0;
virtual list_node& search(const T&) const = 0;
virtual size_t size() const = 0;
};

You can also use "list_node" as parameter of "list_base" template, but it
will be hard to use "list_base" as "common list" interface.

template <typename T, typename list_node>
class list_base
{
public:
virtual ~list_base() {}
virtual void push_back(const T&) = 0;
virtual void push_front(const T&) = 0;
virtual void insert(const list_node *node, const T&) = 0;
virtual void earse(list_node *node) = 0;
virtual void pop_back() = 0;
virtual void pop_front() = 0;
virtual T back() const = 0;
virtual T front() const = 0;
virtual list_node* search(const T&) const = 0;
virtual size_t size() const = 0;
};

template <typename T>class circular_list_node;

template <typename T>
class circular_list : public list_base<T,circular_list_node<T
{
public:
typedef circular_list_node<Tlist_node;

circular_list();
~circular_list();

virtual void push_back(const T&);
virtual void push_front(const T&);
virtual void insert(const list_node *node, const T&);
virtual void earse(list_node *node);
virtual void pop_back();
virtual void pop_front();
virtual T back() const;
virtual T front() const;
virtual list_node* search(const T&) const;
virtual size_t size() const;
private:
circular_list(const circular_list &);
circular_list & operator = (const circular_list &);
list_node *head;
size_t size_;
};

--
Maksim A Polyanin
Jan 27 '07 #4
On 27 Jan 2007 00:39:13 -0800, "Wayne Shu" <Wa******@gmail.comwrote:
>

On Jan 27, 3:29 pm, Dave Rahardja <a...@me.comwrote:
>On 26 Jan 2007 21:21:40 -0800, "Wayne Shu" <Wayne...@gmail.comwrote:
>how could I implement the list node structure?Have you considered having each concrete list type implement its own node
structure? e.g.
the code below can't be compiled, because the list_node in the base
class is an unknown type,
Doh, I forgot to fix that. Here:

template <typename T>
class list_base
{
public:
class list_node
{
public:
virtual T& get_data() const = 0;
};

virtual ~list_base() {}

/* ... */

virtual list_node* search(const T&) const = 0;
};

template <typename T>
class single_list: public list_base<T>
{
public:
class list_node: public list_base<T>::list_node
{
public:
virtual T& get_data() const;
private:
list_node* next;
template <typenamefriend class single_list;
};

/* ... */

virtual list_node* search(const T&) const; // Covariant return type
};

template <typename T>
class double_list: public list_base<T>
{
public:
class list_node: public list_base<T>::list_node
{
public:
virtual T& get_data() const;
private:
list_node* next;
list_node* prev;
template <typenamefriend class double_list;
};

/* ... */

virtual list_node* search(const T&) const; // Covariant return type
};
Jan 27 '07 #5
Grizlyk wrote:
Wayne Shu wrote:
>I have a problem in implementing a common class interface.
Really. There is a problem here

template <
class Tptr,
class Tc_iterator
>
class Vcontainer
{
public:
virtual void add_befor(Tc_iterator&, const Tptr);

virtual ~Vcontainer(){}
};

template <
class Tptr,
class Tc_container
>
class Viterator
{
public:
virtual char next(const char is_check_bound=0);

Tc_container *container;

virtual ~Viterator(){}
};

class Tptr;
Viterator< Tptr, Vcontainer<Tptr,?Viterator a;

How can we point to itself here: ?Viterator .
In theory it can be requested.

--
Maksim A Polyanin

Jan 27 '07 #6
On Jan 28, 12:13 am, Dave Rahardja <a...@me.comwrote:
On 27 Jan 2007 00:39:13 -0800, "Wayne Shu" <Wayne...@gmail.comwrote:
On Jan 27, 3:29 pm, Dave Rahardja <a...@me.comwrote:
On 26 Jan 2007 21:21:40 -0800, "Wayne Shu" <Wayne...@gmail.comwrote:
how could I implement the list node structure?Have you considered having each concrete list type implement its own node
structure? e.g.
the code below can't be compiled, because the list_node in the base
class is an unknown type,Doh, I forgot to fix that. Here:

template <typename T>
class list_base
{
public:
class list_node
{
public:
virtual T& get_data() const = 0;
};

virtual ~list_base() {}

/* ... */

virtual list_node* search(const T&) const = 0;

};template <typename T>
class single_list: public list_base<T>
{
public:
class list_node: public list_base<T>::list_node
{
public:
virtual T& get_data() const;
private:
list_node* next;
template <typenamefriend class single_list;
};

/* ... */

virtual list_node* search(const T&) const; // Covariant return type

};template <typename T>
class double_list: public list_base<T>
{
public:
class list_node: public list_base<T>::list_node
{
public:
virtual T& get_data() const;
private:
list_node* next;
list_node* prev;
template <typenamefriend class double_list;
};

/* ... */

virtual list_node* search(const T&) const; // Covariant return type
why another two function can't be override??
virtual void insert(const list_node *node, const T&);
virtual void earse(list_node *node);
>
};
Jan 28 '07 #7

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

Similar topics

0
by: Brian van den Broek | last post by:
Hi all, IDLE refuses to launch, and I believe it is because I attempted to define a custom key-binding that it doesn't like. I was recently defining a custom keybinding in IDLE 1.1 under...
4
by: Maneesh | last post by:
Maneesh Jun 2, 6:27 pm show options Newsgroups: comp.lang.java.programmer From: "Maneesh" <mmano...@gmail.com> - Find messages by this author Date: 2 Jun 2005 05:57:46 -0700 Local:...
0
by: Ben Cox | last post by:
I need help in defining the C++ tapi interface in C#. Any help with this would be appreciated. #ifndef __ITAMMediaFormat_INTERFACE_DEFINED__ #define __ITAMMediaFormat_INTERFACE_DEFINED__ ...
5
by: Dale | last post by:
Is it possible to declare a method of an interface as static? For instance, can I create an interface that defines a static method and 2 instance methods?
2
by: Chris | last post by:
Hi, I'd like to know if it makes sense to define an event in an interface. Something like: public interface class MyInterface1 { event PropertyChangedEventHandler^ PropertyChanged; } ; In...
2
by: SA | last post by:
Hi all, How could I go about defining ctors in an interface. (not to instantiate the interface, of course, but to force the implementing class to provide a specific ctor). Sub New(ByVal x as...
2
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I use one of 2 arrays dependent on the country. Rather than say: if exchangeID = 1 then dim myPlaceBets() as As UK.exchange.PlaceBets many statements myPlaceBetsReq.bets = myPlaceBets else...
6
by: Arial | last post by:
I have more classes with several common properties. e.g. class A { string Class_A_Prop1{get; set;} string Class_A_Prop2{get; set;} string CommonProp1{get; set;} // common to all classes
5
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= | last post by:
Hi, I would like to have someone comments on what's the best practice defining error codes in C. Here's what I think: solution A: using enum pros: type safe. better for debug (some debugger...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.