Thread: Class inheritance and method issues

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    Class inheritance and method issues

    Hello guys. I have some doubts regarding class inheritance.

    Code:
    class A {
    	
    };
    
    
    class B: public A {
    
    
    };
    So I made "vector<A*>" which contains objects from both class A and B, created as follows:

    Code:
    vector<A*> vec;
    A* obj = new A(); A* obj = new B();

    The main issue is I need to use a method which solely belongs to class B and apply it to "vec" elements.

    The method is only to be applied on elements of the vector created by the following way: A* obj = new B(), which means that only B objects are subject to this method and therefore using shouldn't be illegal.

    Is there any way I can solve the issue explained above? Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Khabz
    The main issue is I need to use a method which solely belongs to class B and apply it to "vec" elements.

    The method is only to be applied on elements of the vector created by the following way: A* obj = new B(), which means that only B objects are subject to this method and therefore using shouldn't be illegal.

    Is there any way I can solve the issue explained above?
    There are a few options. For example, you could use a vector<B*> instead of a vector<A*>. Of course, this means that you can't have A objects, but then if you "need to use a method which solely belongs to class B", then you can't have A objects anyway, unless...

    ... you only "need to use a method which solely belongs to class B" for those objects that are of B, but not A. In that case, you can consider: should the member function actually be in the interface of A instead of only of B? Maybe it does logically belong in A, but is a virtual function that does nothing.

    If absolutely necessary, there's another option: use dynamic_cast to determine if the pointer to A actually points to a B object, and if so, call the member function specific to B. However, this basically means that you've given up on using polymorphism and are just querying objects to figure out if you can call a member function.
    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. Replies: 5
    Last Post: 04-15-2009, 02:47 PM
  2. calling a class method within different class method
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2009, 10:56 AM
  3. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  4. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  5. Method pointers and inheritance
    By Magos in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2006, 06:43 PM