Monday 15 February 2010

c++ - object oriented programming style and pointers to pointers -


I want to create a simple linked list class and other data structures in C ++ to object oriented programming. After editing my old C code I had something like that (I've included one of the functions):
  template   

How it is used:

  int main () {llist & lt; int & gt; * my_integer_list = nullptr; llistAdd (& amp; my_integer_list, 42); lli StAdd (& my_integer_list, 128); llistAdd (& amp; my_integer_list, 1337); (AutoIrr = & amp; my_integer_list; (* IR)! = Nullptr; llistIrAdv (& amp; itr)) cout & lt; Lt; (** IRR). Data & lt; endl; llistClear (& my_integer_list); return 0;}   

The problem that works all perfectly That's how C ++ uses the method of llistAdd (..) instead of OOP-style functions. The problem is that my code is index-to-pointers and point-to-point two pointers Works with (see llistItrAdv (..) ). If I use methods, then I will need to:

  template & lt; Typename T & gt; List and lieutenant; T & gt; * List and lieutenant; T & G: :: Add (Const T & D) {LIlist & lt; T & gt; * Temp = new list & lt; T & gt; (D); Temp- & gt; Next = this; Return temporary; } Int main () {llist & lt; Int & gt; * My_integer_list = nullptr; My_integer_list = my_integer_list-> Add (42); My_integer_list = my_integer_list-> Clear (); Return 0; }   

It again creates ugly codes and is likely to leak memory. There should be a better way of doing this with methods, but I can not really think of anything. I tried to make methods for the indicators, but it is illegal in C ++ Can you learn what OOP-style is like the proper class like the class?

The fact your face has encountered is the fact that your list class is actually Instead of the full list, there is a single node in the linked list, so it does not mean that it will have adding method to see that it is actually also in your non-OOP code - enter list The function does not work on any list object, but on an object (just an indicator here) whose A list is in the context of the object. It suggests a solution to your problem: Rename your old class on llistnode and for a llistnode Create a new list class with the indicator and the new class

  template    

No comments:

Post a Comment