0

I have a code similar to the following:

class News {
public:
  virtual void get() = 0;
}

class Cnn : News {
  void get () {...}
}

class Msnbc : News {
  void get () {...}
}

class Bbc : News {
  void get () {...}
}


main ()
{
 News * news = new Cnn;
 news->get ()

 News * news = new Msnbc;
 news->get ()

 News * news = new Bbc;
 news->get ()
}

Instead of creating each sub-classes, what is the best way to store the sub-classes (Cnn, Msnbc...) and iterate over and get a news from all of the feeds (I can't use STL at a moment)

Thanks

  • 4
    Why can't you use STL? That's like saying you can't use C++ :( Homework? – GManNickG Sep 16 '09 at 21:08
  • 1
    What is the reason of not using STL? You should use a container class - even if homegrown - to manage the allocation. (RAII - http://stackoverflow.com/questions/712639/please-help-us-non-c-developers-understand-what-raii-is). If that's not an option either, it's `new News *[]` – peterchen Sep 16 '09 at 21:09
  • 2
    The code is running on embedded device, and the linker generates error when STL is used. It has to do something with IO. –  Sep 16 '09 at 21:14
  • 1
    STL is going to be a timesaver on any major project you write - unless you really want to do unit tests for your custom linked-list classes. – quillbreaker Sep 16 '09 at 21:15
  • I know the pain of C++ on embedded - it's like being catapulted back into the early '90. Still, I'd strongly recommend to try to resolve the STL problem – peterchen Sep 17 '09 at 12:17

2 Answers2

2

If you can't use STL then your best bet is to use an array of News*.

News*[] GetAllNews( int& count ) {
  News*[] arr = new News*[3];
  count = 3;
  arr[0] = new Cnn();
  arr[1] = new Msnbc();
  arr[2] = new Bbc();
  return arr;
}

int count;
News*[] arr = GetAllNews(count);
for ( int i = 0; i < count; i++ ) {
  arr[i]->get();
}

// Now for the awkward cleanup
for ( int i = 0; i < count; i++ ) {
  delete arr[i];
  arr[i] = NULL;
}
delete[] arr;
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • I know that many will disagree with me, but you (a) do at runtime what should've been at compile time and (b) being way too verbose. – Michael Krelin - hacker Sep 16 '09 at 21:12
  • 1
    @hacker, you went for a more static solution while I went for a more dynamic one. The dynamic one is necessarily more verbose. – JaredPar Sep 16 '09 at 21:13
  • That's the point - your code looks like it is translated from dynamic language -- c++ isn't your native, is it? ;-) Actually, I'd say you're too verbose for completely dynamic way too. Again - it is my opinion, which many will disagree with. – Michael Krelin - hacker Sep 16 '09 at 21:17
  • 2
    @hacker: I'd say that a gold badge in C++ qualifies JaredPar as a non-beginner in the language. Then again, he's not litb :P – David Rodríguez - dribeas Sep 16 '09 at 21:50
  • 1
    @hacker, how can you have a dynamically sized array (no-STL) and be less verbose? Barring some template insanity. – JaredPar Sep 16 '09 at 22:01
  • JaredPar, first, I see no reason to bar what you call template insanity (although where would I put it here?), but, anyway, on a second thought I think I'd take my word on verbosity for completely dynamic implementation back. – Michael Krelin - hacker Sep 17 '09 at 07:05
  • 1
    dribeas, golden badge in c++ means he's been upvoted by people who infer from my words he *is* a beginner in the language. So, badge signifies nothing at all - it's not Jared's fault on the other hand that he's got you and two of your upvoters for defenders ;-) And I have no idea what "litb" is. Even though I'm *not* a beginner in the language, English is not my native and will never be;-) – Michael Krelin - hacker Sep 17 '09 at 07:11
  • @hacker: I had to upvote your last comment for the humour value of "And I have no idea what 'litb' is"... :) litb's a guy on SO with a level of C++ knowledge that borders on scary. Some say he is half human, half template specialisation... :) – j_random_hacker Sep 17 '09 at 08:22
  • 1
    @JaredPar: Nothing verbose that I can see here, but I think your array declarations are a little off -- should be `News* arr[]` instead of `News*[] arr`, and the return type of `GetAllNews()` needs to change similarly. – j_random_hacker Sep 17 '09 at 08:24
  • j_random_hacker, ;-) Despite my registration date I'm here only for one month and don't really spend all the time on SO. Thanks for education - I thought it's yet another cause of PCMCIA (people can't memorize computer industry acronyms). And yes, I took back my words on verbosity for dynamic approach, it was just an impression. I don't think that severely insulted Jared, at the time of my first comment I considered his answer totally valid, anyway. I regret having commented on it. – Michael Krelin - hacker Sep 17 '09 at 09:13
  • Downvoting to get your attention re: array syntax (I'd fix it myself, but I can't be bothered figuring out the correct syntax for the declaration of `GetAllNews()`... :)) – j_random_hacker Sep 19 '09 at 02:53
  • @hacker: Yes you're right there. Since Jared is doing 2-level dynamic allocation, he needs a `News**` instead of an array-of-pointer-to-`News` -- otherwise the 2nd line in his snippet doesn't make sense. – j_random_hacker Sep 20 '09 at 09:48
  • Now that I've looked at the code again I know what verbosity bothers me ;-). I'd change the second line to `News **arr = new News*[count=3]`. For the sake of maintainability. Having to keep the list of assignments in sync with allocation count is enough pain, I wouldn't want to have to change the number twice. – Michael Krelin - hacker Sep 20 '09 at 14:08
0
News *n[] = { new Cnn(), new Msnbc(), new Bbc() };
for(News **p=n;p<&n[ sizeof(n)/sizeof(*n) ];++p) {
    (*p)->get();
    delete *p;
}
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • Why the downvotes? It maybe concise as answer, but it answers the question. The static approach is valid too, especially in smaller embedded systems that can't use the complete C++. – stefaanv Sep 17 '09 at 08:16
  • Come on guys, this is a good answer -- concise and correct. +1. (Would be nice to split the `sizeof` element-count trick into a separate inline function template for clarity mind you.) – j_random_hacker Sep 17 '09 at 08:29
  • Thanks guys. j_random_hacker, yes, in real life code that would make sense, but in the answer I think it gives better idea of what's going on. At least that was my motivation for embedding it here. – Michael Krelin - hacker Sep 17 '09 at 09:03
  • stefaanv, I actually had no idea what to add to the code here - it's so short that needs no comments that do not repeat the code itself. As for static approach, *in this particular case* I see no single reason to prefer dynamic over it. It is faster (well, not much, of course), easier to maintain, shorter... – Michael Krelin - hacker Sep 17 '09 at 09:07