Pure virtual functions: declaring a function in a base class

Discussion in 'C++' started by Martin, Aug 25, 2010.

  1. Martin

    Martin Guest

    Why doesn't this work? Is there a way to achieve it?
    Compiler complains about func1() not being defined in class C, which
    inherits it from class B.

    class A
    {
    public:
    virtual void func1() = 0;
    }

    class B
    {
    public func1()
    {
    printf("test");
    }

    };

    class C : public B, public A
    {

    };

    int _tmain(int argc, _TCHAR* argv[])
    {
    C c;
    c.func1();
    return 0;
    }
     
    Martin, Aug 25, 2010
    #1
    1. Advertising

  2. * Martin, on 25.08.2010 05:11:
    > Why doesn't this work? Is there a way to achieve it?
    > Compiler complains about func1() not being defined in class C, which
    > inherits it from class B.
    >
    > class A
    > {
    > public:
    > virtual void func1() = 0;
    > }
    >
    > class B
    > {
    > public func1()
    > {
    > printf("test");
    > }
    >
    > };
    >
    > class C : public B, public A
    > {
    >
    > };
    >
    > int _tmain(int argc, _TCHAR* argv[])
    > {
    > C c;
    > c.func1();
    > return 0;
    > }


    Presumably you want the implementation in B to override the one in A.

    And although you're not posting your real code, the typing mistakes that you've
    made seem to indicate that you come from a Java background. Which is helpful in
    helping you. But in general, don't assume that your readers are telepaths and
    can see what's only in your head or on your screen: that's just stupid. Post
    *real code*. See the FAQ about how to ask about Code That Does Not Work.

    Now, B::func1 is not virtual, so it can't override anything.

    However, if you make B::func1 virtual then you still don't get an override, only
    a call ambiguity. The compiler sees 2 possibilities for which function you're
    trying to call. And nothing decides between them.

    You can either

    * add an override in class C, e.g. calling the B::func1, or

    * make A an abstract class, which means change the inheritance so that C
    inherits B inherits A (in this case func1 becomes virtual in B since it's
    already virtual in A, and B::func1 becomes an override of A::func1), or

    * make A a logical interface, which means let B inherit virtually from A and
    let C inherit virtually from A, and in this case B implements the A
    interface A for class C, at some run time and design level cost.

    That said, there is no such thing as '_tmain' in C++. It's a Microsoft
    monstrosity, and moreover it's a monstrosity (only) in support of Windows 9x,
    which one may presume that you're not out to support? Use a standard 'main'.

    You don't use the 'main' arguments so no point in declaring them.

    Finally you can omit the 'return 0', since 'main' returns 0 by default.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Aug 25, 2010
    #2
    1. Advertising

  3. Martin

    Martin Guest

    Hey Alf,
    Thanks for the reply.
    I don't see why I should declare a function "virtual" to override
    a virtual function, I noly need to declare it virtual in the base
    class, not in the derived class, but maybe I misunderstood what you
    said.
    That being said, I still don't understand why the compiler won't
    take B's func1 as an override for class A. I understand your proposals
    and agree with them, but I would like to understand why the compiler
    won't override func1(). Maybe I just need to go back and read about c+
    + compiler implementation...

    You understood the problem perfectly, I'll go and read the FAQ
    about posting...

    Thanks,
    Martín.





    On Aug 25, 12:35 am, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * Martin, on 25.08.2010 05:11:
    >
    >
    >
    >
    >
    > > Why doesn't this work? Is there a way to achieve it?
    > > Compiler complains about  func1() not being defined in class C, which
    > > inherits it from class B.

    >
    > > class A
    > > {
    > > public:
    > >    virtual void func1() = 0;
    > > }

    >
    > > class B
    > > {
    > >    public func1()
    > >    {
    > >            printf("test");
    > >    }

    >
    > > };

    >
    > > class C : public B, public A
    > > {

    >
    > > };

    >
    > > int _tmain(int argc, _TCHAR* argv[])
    > > {
    > >    C c;
    > >    c.func1();
    > >    return 0;
    > > }

    >
    > Presumably you want the implementation in B to override the one in A.
    >
    > And although you're not posting your real code, the typing mistakes that you've
    > made seem to indicate that you come from a Java background. Which is helpful in
    > helping you. But in general, don't assume that your readers are telepaths and
    > can see what's only in your head or on your screen: that's just stupid. Post
    > *real code*. See the FAQ about how to ask about Code That Does Not Work.
    >
    > Now, B::func1 is not virtual, so it can't override anything.
    >
    > However, if you make B::func1 virtual then you still don't get an override, only
    > a call ambiguity. The compiler sees 2 possibilities for which function you're
    > trying to call. And nothing decides between them.
    >
    > You can either
    >
    >    * add an override in class C, e.g. calling the B::func1, or
    >
    >    * make A an abstract class, which means change the inheritance so that C
    >      inherits B inherits A (in this case func1 becomes virtual in B since it's
    >      already virtual in A, and B::func1 becomes an override of A::func1), or
    >
    >    * make A a logical interface, which means let B inherit virtually from A and
    >      let C inherit virtually from A, and in this case B implements the A
    >      interface A for class C, at some run time and design level cost.
    >
    > That said, there is no such thing as '_tmain' in C++. It's a Microsoft
    > monstrosity, and moreover it's a monstrosity (only) in support of Windows 9x,
    > which one may presume that you're not out to support? Use a standard 'main'.
    >
    > You don't use the 'main' arguments so no point in declaring them.
    >
    > Finally you can omit the 'return 0', since 'main' returns 0 by default.
    >
    > Cheers & hth.,
    >
    > - Alf
    >
    > --
    > blog at <url:http://alfps.wordpress.com>- Hide quoted text -
    >
    > - Show quoted text -
     
    Martin, Aug 25, 2010
    #3
  4. Martin

    Felix Palmen Guest

    * Martin <>:
    > I don't see why I should declare a function "virtual" to override
    > a virtual function, I noly need to declare it virtual in the base
    > class, not in the derived class, but maybe I misunderstood what you
    > said.


    I don't think this makes much sense. Assume a function X that is virtual
    in class A, non-virtual in class B and overriden in class C with
    inheritance graph A -> B -> C.

    What should happen when you call X() on an instance of C through a
    pointer of type A? You think B::X() should be invoked? In my opinion,
    this would be ill-defined.

    > That being said, I still don't understand why the compiler won't
    > take B's func1 as an override for class A. I understand your proposals
    > and agree with them, but I would like to understand why the compiler
    > won't override func1(). Maybe I just need to go back and read about c+
    > + compiler implementation...


    The notion of "overriding" implies inheritance. In your example, you
    present a class that happens to comprise a function with the same name
    as another class. But as these classes do not have any relation to each
    other, the functions are unrelated as well.

    Any other assumption would violate agnosticism. Without any interface
    implementation or inheritance, your class would need knowledge about
    derived classes to know that it's method should be an implementation of
    a virtual method in another class.

    So, I don't think this is related to compiler implementation, more to
    language sematics.

    Regards, Felix

    --
    Felix Palmen (Zirias) + [PGP] Felix Palmen <>
    web: http://palmen-it.de/ | http://palmen-it.de/pub.txt
    my open source projects: | Fingerprint: ED9B 62D0 BE39 32F9 2488
    http://palmen-it.de/?pg=pro + 5D0C 8177 9D80 5ECF F683
     
    Felix Palmen, Aug 25, 2010
    #4
  5. Martin

    mingze zhang Guest

    It is very hard to guess what you do know and what you don't know from
    your questions..
    What you probably wanted to do is this,

    class A
    {
    public:
    virtual void func1() = 0;
    }

    class B : public A
    {
    public:
    void func1()
    {
    printf("test");
    }
    };

    class C : public B
    {
    };

    int _tmain(int argc, _TCHAR* argv[])
    {
    C c;
    c.func1();
    return 0;
    }
     
    mingze zhang, Aug 26, 2010
    #5
  6. Martin

    Felix Palmen Guest

    * mingze zhang <>:
    > It is very hard to guess what you do know and what you don't know from
    > your questions..


    Please, take some care about thread structure when posting. I did not
    ask any question here.

    Regards, Felix
    (f'up2 poster)

    --
    Felix Palmen (Zirias) + [PGP] Felix Palmen <>
    web: http://palmen-it.de/ | http://palmen-it.de/pub.txt
    my open source projects: | Fingerprint: ED9B 62D0 BE39 32F9 2488
    http://palmen-it.de/?pg=pro + 5D0C 8177 9D80 5ECF F683
     
    Felix Palmen, Aug 26, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    730
    Richard Herring
    Oct 18, 2005
  2. John Goche
    Replies:
    10
    Views:
    770
    Marcus Kwok
    Dec 8, 2006
  3. Arne Schmitz
    Replies:
    4
    Views:
    438
    Daniel Albuschat
    Jan 17, 2007
  4. Replies:
    2
    Views:
    570
  5. gwowen
    Replies:
    6
    Views:
    332
    MikeWhy
    Jan 18, 2012
Loading...

Share This Page