Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7888587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:52:11+00:00 2026-06-03T05:52:11+00:00

UML Diagram : https://i.stack.imgur.com/ajsVQ.png My OOP teacher said that we could implement something like

  • 0

UML Diagram : https://i.stack.imgur.com/ajsVQ.png

My OOP teacher said that we could implement something like in the above diagram in C++ (for a lab assignment). She said we could do something like this so we wouldn’t need two List classes (one for Books and one for Clients).

I started implementing it and i’m stuck on getFromPosition(int) (it is supposed to return the object at certain position in the list). What I am trying to do is to save a Book object in a List object (this works fine) and i would later like to be able to return/read/modify these saved Book objects.It returns the object , but then I (probably) should make a cast of some sort so I could use the methods of the Book. Is this even possible?
This is the code for the method:

   Obj List::getFromPosition(int i){
            return list[i];
        }

I hope I gave enough info (this is my first question ).
Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:52:13+00:00Added an answer on June 3, 2026 at 5:52 am

    Since you’re returning an Obj by value, I’m guessing internally you hold a collection of Obj objects, and not pointers.

    Which is wrong. You run into object slicing.

    Firstly, you should keep pointers or smart pointers internally, and return an Obj* or a SmartPtr<Obj>.

    Second, whether you need the cast or not depends.

    If you have a common method between your deriving classes, it should probably go in the base class. If not, you probably need a cast.

    Here’s how I’d do it rough cut:

    class List
    {
       std::vector<Obj*> _objects;
    public:
       Obj* getFromPosition(int i)
       {
          return _objects[i];
       }
    };
    
    //...
    List list;
    //...
    Obj* obj = list.getFromPosition(0);
    
    obj->commonMethod();   //common method is virtual and a member of Obj
    (dynamic_cast<Book*>(obj))->bookMethod(); //book method is only available for Book class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://www.dofactory.com/Patterns/PatternFlyweight.aspx There is a UML diagram on that page. It has three different kinds
What kind of UML diagram is this? (source: tripod.com )
The diagram http://www.freeimagehosting.net/uploads/2fd3f4161c.png Here's the Minimalist-UML diagram of an app I've been working on.
I am trying to understand UML diagram describing Decorator Pattern at link below http://www.dofactory.com/Patterns/PatternDecorator.aspx
In the following UML diagram that represents a Decorator pattern for designing games character
I would like to find the best way to parse an UML Diagram that
In this beginners guide to Dependency Injection I noticed that the UML diagram distinguishes
Here's an UML diagram depicting a hypergraph. It was taught to me that a
I am trying to implement an UML Class Diagram and I would like to
I'm just using StarUML to create my UML Class diagram and something is bothering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.