Two conceptual questions

This is a discussion on Two conceptual questions within the C++ Programming forums, part of the General Programming Boards category; Hi there, I was trying to summary some basic conceptions about class inheritance and I am not 100% sure about ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    13

    Two conceptual questions

    Hi there, I was trying to summary some basic conceptions about class inheritance and I am not 100% sure about my answer to the following two questions. Please point out if my answer is wrong. And any suggestions and comments appreciated

    1. if the base class uses "new" to dynamic allocate memory
    the copy constructor for the base class is marked as protected and
    the derived class doesn't have any member which needs dynamic memory allocation
    does the derived class need an explicit copy constructor? //NO
    Also, the copy assignment operator for base class should be marked as public and the derived class doesn't need an explicit copy assignment operator declaration

    2. a declaration of function with same name and different signature in the derived class can hide functions defined in base class with same name. In following two situations:
    Situation1:
    Code:
    //base class
    class base{
      public: 
          virtual void func();    //#1
          virtual void func(int);   //#2
          virtua void func(int, int);   //#3
    //...
    };
    
    //derived class A
    class A: public base{
      public:
          virtual void func(int, int, int)
    //...
    };
    #1, #2, #3 functions are all hiden in the derived class
    then, for a pointer or reference of the base class (which may point or refer to the derived class object), ptr or rf, are the following statement valid?
    Code:
          ptr->func(); //#4 invalid   
          ra.func(3, 5); //#5 invalid
    also if class B derived from classA,which may define functions like #1, #2, #3, even, ptr actually points the a classB object, statements #4 and #5 will be still invalid.

    Situation 2:
    Code:
    //base class
    class base{
      public
          virtual void func();    //#1
          virtual void func(int);   //#2
          virtua void func(int, int);   //#3
    };
    
    //derived class
    class A : public base{
    void func(int)
    };
    if the derived class just redefines #2,. will #1 and #3 inherited? //yes
    are statements #4 and #5 valid?
    ptr->func(); //valid
    ra.func(3, 5); //valid
    also , can a derived class object still invoke function #1 and #3? //yes
    Last edited by AntiScience; 11-01-2007 at 09:27 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    1. if the base class uses "new" to dynamic allocate memory
    the copy constructor for the base class is marked as protected and
    the derived class doesn't have any member which needs dynamic memory allocation
    does the derived class need an explicit copy constructor? //NO
    Also, the copy assignment operator for base class should be marked as public and the derived class doesn't need an explicit copy assignment operator declaration
    I would expect in such situations it is the base class that needs both a copy constructor and an assignment operator (unless instances of the whole hierarchy are not meant to be copyable). Then, if copying the derived class's part is trivial, it shouldn't need any special treatment.

    2. a declaration of function with same name and different signature in the derived class can hide functions defined in base class with same name...
    You can access the base class versions through a base pointer and the derived class func version through a pointer to derived instance.
    In addition, given a derived instance, you should be able to call the base class versions by explicitly stating so:
    Code:
        A a;
        a.base::func();
    However, having shadowed virtual functions kind of seems to defeat the purpose of a virtual function, making it hard to make truly polymorphic function calls.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Shadowing virtual functions is considered very bad practice in general.

    That said, I have a hard time knowing where the questions and your answers are in your post.

    Shadowing is a matter of lookup. Given base, A and B from situation 1:
    Code:
    B *pb = new B();
    A *pa = pb;
    base *pbas = pa;
    
    // Too lazy to correct my typo from foo to func now.
    pb->func(); // Valid, calls B::foo()
    pb->func(1); // Valid, calls B::foo(int)
    pb->func(1, 2); // Valid, calls B::foo(int, int)
    pb->func(1, 2, 3); // Invalid, A::foo(int, int, int) is hidden
    
    pa->func(); // Invalid, base::foo() is hidden
    pa->func(1); // Invalid, base::foo(int) is hidden
    pa->func(1, 2); // // Invalid, base::foo(int, int) is hidden
    pa->func(1, 2, 3); // Valid, calls A::foo(int, int, int)
    
    pa->base::func(1, 2); // Valid, but beware! Calls base::func(int, int), not B::func(int, int)!
    
    pbas->func(); // Valid, calls B::foo()
    pbas->func(1); // Valid, calls B::foo(int)
    pbas->func(1, 2); // Valid, calls B::foo(int, int)
    pbas->func(1, 2, 3); // Invalid, No such function in base.
    For situation 2:
    Code:
    pa->func(); // Invalid, base::func() is hidden
    pa->func(1); // Valid, calls A::func(int)
    pa->func(1,2); // Invalid, base::func(int, int) is hidden
    
    pbas->func(); // Valid, calls base::func()
    pbas->func(1); // Valid, calls A::func(int)
    pbas->func(1, 2); // Valid, calls base::func(int, int)
    Last edited by CornedBee; 11-01-2007 at 12:41 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    13
    thank both of you for replies.
    That makes sense to me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21