CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Constructor Inheritance

    Been playing with virtual copy assignment operators. Although in Base properties recognizes the operator as having the 'IsVirtual' property true, I cant find any signature in Derived that doesn't include the virtual keyword for which the same property is true. This is making it look like copy assignment is not inherited and virtual is all but ignored. How should a virtual copy assignment operator behave? It really doesn't make any sense to me.
    As for virtual destructors as LL pointed out, the slot in the vtable is inherited but the destructor itself isn't. The base class destructor will be called implicitly by the compiler so it has to be accessible to the derived class but at no time does Base::~Base become a member of Derived. The standard allows explicitly calling destructors( basically only for placement new use) but does not allow the same latitude with constructors.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  2. #17
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Constructor Inheritance

    Been playing with virtual copy assignment operators. Although in Base properties recognizes the operator as having the 'IsVirtual' property true, I cant find any signature in Derived that doesn't include the virtual keyword for which the same property is true. This is making it look like copy assignment is not inherited and virtual is all but ignored. How should a virtual copy assignment operator behave? It really doesn't make any sense to me.
    Speaking of which, what exactly is an override of a virtual copy assignment operator? My intuition leads me to:
    Code:
    #include <iostream>
    
    class X
    {
    public:
        virtual X& operator=(const X& other)
        {
            std::cout << "X copy assignment" << std::endl;
            return *this;
        }
    
        virtual ~X() {}
    };
    
    class Y : public X
    {
    public:
        Y& operator=(const X& other)
        {
            std::cout << "Y override of X copy assignment" << std::endl;
            return *this;
        }
    };
    
    class Z : public X
    {
    public:
        Z& operator=(const X& other)
        {
            std::cout << "Z override of X copy assignment" << std::endl;
            return *this;
        }
    };
    
    int main()
    {
        Y y;
        Z z;
        X& r = y;
        r = z;
    }
    With the MinGW port of g++ 3.4.5, I get the output of "Y override of X copy assignment", which is exactly what I would expect if some other virtual member function was involved. That also makes the most sense to me concerning how a virtual copy assignment operator should behave, if we can consider them to be copy assignment operators even though the parameters are of type reference to the base class rather than reference to the current class.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #18
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Constructor Inheritance

    Hmm maybe an IDE bug in msvc10. The function behaves virtually so I guess must be an inheritable member unless virtual copy assignment operates in the same way as virtual destructors, but my IDE still insists Y::operator =(const X&) is a nonvirtual but then if I add trivial destructors to Y and Z it also insists they are non-virtual.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  4. #19
    Join Date
    Aug 2007
    Posts
    858

    Re: Constructor Inheritance

    Quote Originally Posted by Ajay Vijay View Post
    Not a bug. It compiles in VC6, VC7, VC7.1, VC8, VC9 and VC10 as well. I do not know about standard as such, and never used Comeau compiler.
    I'd say it should be considered a bug. The standard states that ctors have neither names nor addresses, and that they should only be used to initialize an object. MSVC is allowing you to explicitly call a ctor and run a ctor on an object that's already been constructed.

  5. #20
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Constructor Inheritance

    Quote Originally Posted by Speedo
    I'd say it should be considered a bug.
    I would not call it as bug, but non-compliance to the standard.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  6. #21
    Join Date
    Jul 2007
    Posts
    249

    Re: Constructor Inheritance

    Quote Originally Posted by Lindley View Post
    The base constructor is accessible, yes, but it is not inherited because this will not work:
    Ohk I got it.
    So there are things that will be accessible but not inherited like constructors.

    I got another problem just related to accessibility like below
    Code:
    class Base{
    int x;
    };
    
    class Derived : public Base{
    int y;
    };
    if i see the sizeof (Derived) it is giving me 8. (consider int size is 4)
    But I know that private variables are never inherited.

    But is it like it comes to the Derived class but is not accessible and adds 4 bytes to the size.
    Correct me if I am wrong

  7. #22
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Constructor Inheritance

    Quote Originally Posted by Rajesh1978 View Post
    But I know that private variables are never inherited.
    Not quite true. Inheritance is an IS-A relationship, so a Derived IS-A Base. That means that the Derived must contain all members of Base.

    However, some of those members, specifically the private ones, may not be accessible to the methods of Derived. So what good are they?

    Well, keep in mind that some methods of Base may be inherited wholesale, both signature and implementation. Any public or protected method of Base which is not virtual fits this category, as well as any virtual methods which are not overridden by Derived. These methods will be exposed as part of Derived and accessible, but since they're actually Base functions, they're able to access the private members of Base just fine.

  8. #23
    Join Date
    Jul 2007
    Posts
    249

    Re: Constructor Inheritance

    Whatever there in the Base will come to Derived and increase the size(if attribute) as I showed just before.
    Irrespctive of whether it is private or public.

    private variables of Base are not accessible directly in the child but we can access it indirectly.

    Am I correct

  9. #24
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Constructor Inheritance

    Private members are inherited, they are not accessible.
    For example:
    Code:
    Derived d;
    int* p= (int*)&d;
     
    *p=10; // Sets Base::x to 10
    Though, it should not be used!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured