Thread: what design patter let me reuse this class?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    what design patter let me reuse this class?

    Hey , there,

    I though it is an easy one, but I am stuck here: I have a class like this:
    Code:
    class Circulararray(){
    int l;
    double* data;
    public:
         Circulararray(int length): l(length){
          data = new double[l];
        }
       ~Circulararray(){delete[] data;}
    
       double getvalueatidx(int idx){...}
      
      void putatfront(double value){...}
    }
    now I want to reuse it for time_t value type, i.e., the data[] inside the class shall be time_t array, and the method getvalueatidx(int idx) should return time_t as well.

    how to design and reuse?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you would like to turn your class into a class template?

    EDIT:
    Ah, but you have lots more to do even without introducting templates. Have you considered storing a std::vector or some other suitable container instead of manually managing memory?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    48
    YES, I thought of using an standard one, but didn't find it. I need a circular queue or linked list, which doesn't seems to be in the std. so I start to make one myself.

    I probably can use std::deque. But I am not sure about how it manages its size. If I am doing this, can I assume its size is constant if I always pop_front() and then push_back()?

    The constant size (or not wildly goes up) is important for me, since I will do the push_back() and pop_front() every second for several days.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If that is all the class supports, then the best "design pattern" IMO is to use existing tools like std::deque or std::list.

    (And if you are comfortable with using those, you wouldn't have this question any more )

    Edit:
    The constant size (or not wildly goes up) is important for me, since I will do the push_back() and pop_front() every second for several days.
    This sounds like a queue to me, so you could even use the std::queue adapter (which should let you choose the underlying container type).

    I myself have wondered whether a std::deque would keep "crawling" through memory space when you do what you describe, or would it reuse the memory block that has become free at the beginning of it.
    Last edited by anon; 04-29-2009 at 01:49 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That reminds me, I think CornedBee once wrote an iterator adapter for a ring buffer using a standard container.

    EDIT:
    Ah, posted in dudeomanodude's Circular Lists thread.
    Last edited by laserlight; 04-29-2009 at 01:47 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 10:16 AM
  2. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. class design and operation overload
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2001, 10:49 PM

Tags for this Thread