473,661 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(cons t 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(cons t 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(c onst 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 1900
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(cons t 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_lis t : public list_base<T>
{
public:
/* ... */

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

template <typename T>
class double_link_lis t : 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.comwro te:
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(cons t 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_lis t : public list_base<T>
{
public:
/* ... */

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

};template <typename T>
class double_link_lis t : 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(cons t 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(cons t 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(cons t 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_n ode;

template <typename T>
class circular_list : public list_base<T,cir cular_list_node <T
{
public:
typedef circular_list_n ode<Tlist_node;

circular_list() ;
~circular_list( );

virtual void push_back(const T&);
virtual void push_front(cons t 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(c onst 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.comwro te:
>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>::l ist_node
{
public:
virtual T& get_data() const;
private:
list_node* next;
template <typenamefrie nd 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>::l ist_node
{
public:
virtual T& get_data() const;
private:
list_node* next;
list_node* prev;
template <typenamefrie nd 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_it erator&, 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.comwro te:
On 27 Jan 2007 00:39:13 -0800, "Wayne Shu" <Wayne...@gmail .comwrote:
On Jan 27, 3:29 pm, Dave Rahardja <a...@me.comwro te:
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>::l ist_node
{
public:
virtual T& get_data() const;
private:
list_node* next;
template <typenamefrie nd 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>::l ist_node
{
public:
virtual T& get_data() const;
private:
list_node* next;
list_node* prev;
template <typenamefrie nd 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
1398
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 Python 2.4 on WinMe. (I was using the simpler of the two binding definition interfaces.) When done, I hit the buttons for 'OK' (or however it is labelled -- I cannot currently check), but the definition dialog remained open. After several tries, I hit...
4
1593
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: Thurs,Jun 2 2005 6:27 pm Subject: How to define common interface between C and Java modules. Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse
0
1669
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__ /* interface ITAMMediaFormat */ /* */
5
4333
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
1035
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 particular, what will happen if a class derives from two interfaces
2
1003
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 MyType) doesn't work. May be it's not possible, in which case it should be on the to-do list for future versions...
2
1667
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 dim myPlaceBets() As AU.exchange.PlaceBets many statements
6
1826
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
13483
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 will show the name not only the value) cons: enum can not be forward declared which makes all error codes
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.