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]

2
  • What is your question? Commented Jul 9, 2020 at 10:04
  • 1
    Please don't use such sites as a learning resource, because they're not. Get some good C++ books 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. Commented Jul 9, 2020 at 10:04

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.