0

When do you want to use a dynamically allocated object such as, https://leetcode.com/problems/design-twitter/,

  • Your Twitter object will be instantiated and called as such:
  • Twitter* obj = new Twitter();
  • obj->postTweet(userId,tweetId);
  • vector param_2 = obj->getNewsFeed(userId);
  • obj->follow(followerId,followeeId);
  • obj->unfollow(followerId,followeeId);

[ie pointer to a new object]

or a (static?) object such as, https://leetcode.com/problems/flatten-nested-list-iterator/:

  • Your NestedIterator object will be instantiated and called as such:
  • NestedIterator i(nestedList);
  • while (i.hasNext()) cout << i.next();

[ie standard object initialization]

Trajan
  • 1,380
  • 6
  • 20
  • 41
  • What is your question? – Yksisarvinen Jul 09 '20 at 10:04
  • 1
    Please don't use such sites as a learning resource, because they're not. Get [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read if you want to learn C++. It should hlep you understand when and where to use "dynamic objects". I also recommend taking a few classes, and learn common algorithms and data structure, discrete math, and other programming languages, if you want to become more than a simple code moneky. – Some programmer dude Jul 09 '20 at 10:04

1 Answers1

1

You usually dynamically allocate objects if you do not know upfront how many of those you will need or if objects instances need to be shared between users. As a side note, never use new use the std::unique_ptr or std::shared_ptr instead.

Jean-Marc Volle
  • 3,113
  • 1
  • 16
  • 20