pure member functions

Discussion in 'C++' started by Rahul, Dec 18, 2007.

  1. Rahul

    Rahul Guest

    Hi,

    I tried to create a abstract class by having a non-virtual member
    function as pure, but i got a compilation error saying "only virtual
    member functions can be pure"...

    I'm trying to think the reason behind this restriction... i just want
    to want a base class to be abstract so as to avoid object slicing into
    the base type from derived type, and in my case base class doesn't
    need to have a virtual function.

    Is there any alternative? and what is the reason behind only virtual
    member functions being pure?
     
    Rahul, Dec 18, 2007
    #1
    1. Advertising

  2. On Dec 18, 11:52 pm, Rahul <> wrote:
    > Hi,
    >
    > I tried to create a abstract class by having a non-virtual member
    > function as pure, but i got a compilation error saying "only virtual
    > member functions can be pure"...
    >
    > I'm trying to think the reason behind this restriction... i just want
    > to want a base class to be abstract so as to avoid object slicing into
    > the base type from derived type, and in my case base class doesn't
    > need to have a virtual function.
    >
    > Is there any alternative? and what is the reason behind only virtual
    > member functions being pure?


    If you don't have any virtual function, probably atleast you will have
    a virtual destructor. Make it pure, provide an implementation. IF a
    non-virtual function is pure - how would you override it? And if you
    will not override it, what does it mean? Nothing. So, no use having
    such members.
     
    Abhishek Padmanabh, Dec 18, 2007
    #2
    1. Advertising

  3. Rahul

    Kira Yamato Guest

    On 2007-12-18 13:52:49 -0500, Rahul <> said:

    > Hi,
    >
    > I tried to create a abstract class by having a non-virtual member
    > function as pure, but i got a compilation error saying "only virtual
    > member functions can be pure"...
    >
    > I'm trying to think the reason behind this restriction... i just want
    > to want a base class to be abstract so as to avoid object slicing into
    > the base type from derived type, and in my case base class doesn't
    > need to have a virtual function.


    I don't understand your goal. What do you mean by "object slicing into
    the base type from derived type?"

    >
    > Is there any alternative? and what is the reason behind only virtual
    > member functions being pure?


    --

    -kira
     
    Kira Yamato, Dec 18, 2007
    #3
  4. Rahul

    Rahul Guest

    On Dec 18, 11:56 pm, Abhishek Padmanabh <>
    wrote:
    > On Dec 18, 11:52 pm, Rahul <> wrote:
    >
    > > Hi,

    >
    > > I tried to create a abstract class by having a non-virtual member
    > > function as pure, but i got a compilation error saying "only virtual
    > > member functions can be pure"...

    >
    > > I'm trying to think the reason behind this restriction... i just want
    > > to want a base class to be abstract so as to avoid object slicing into
    > > the base type from derived type, and in my case base class doesn't
    > > need to have a virtual function.

    >
    > > Is there any alternative? and what is the reason behind only virtual
    > > member functions being pure?

    >
    > If you don't have any virtual function, probably atleast you will have
    > a virtual destructor. Make it pure, provide an implementation. IF a
    > non-virtual function is pure - how would you override it? And if you
    > will not override it, what does it mean? Nothing. So, no use having
    > such members.


    Yes, you are probably referring to current standards and
    implementation of c++,
    however, when the standard was being developed, some one could have
    thought of pure ordinary member functions and a mechanism to override
    them too... for some reason they haven't done that... which is what i
    was wondering about...
     
    Rahul, Dec 18, 2007
    #4
  5. Rahul

    Jim Langston Guest

    Rahul wrote:
    > Hi,
    >
    > I tried to create a abstract class by having a non-virtual member
    > function as pure, but i got a compilation error saying "only virtual
    > member functions can be pure"...
    >
    > I'm trying to think the reason behind this restriction... i just want
    > to want a base class to be abstract so as to avoid object slicing into
    > the base type from derived type, and in my case base class doesn't
    > need to have a virtual function.
    >
    > Is there any alternative? and what is the reason behind only virtual
    > member functions being pure?


    If a member function is pure, it needs to be virtual so it can be
    overridden. Otherwise a derived class can't override it and so a method of
    the class can never be declared. Without being virtual the best that can be
    done is name hiding which isn't quite the same as overriding.

    --
    Jim Langston
     
    Jim Langston, Dec 18, 2007
    #5
  6. On 2007-12-18 20:02, Rahul wrote:
    > On Dec 18, 11:56 pm, Abhishek Padmanabh <>
    > wrote:
    >> On Dec 18, 11:52 pm, Rahul <> wrote:
    >>
    >> > Hi,

    >>
    >> > I tried to create a abstract class by having a non-virtual member
    >> > function as pure, but i got a compilation error saying "only virtual
    >> > member functions can be pure"...

    >>
    >> > I'm trying to think the reason behind this restriction... i just want
    >> > to want a base class to be abstract so as to avoid object slicing into
    >> > the base type from derived type, and in my case base class doesn't
    >> > need to have a virtual function.

    >>
    >> > Is there any alternative? and what is the reason behind only virtual
    >> > member functions being pure?

    >>
    >> If you don't have any virtual function, probably atleast you will have
    >> a virtual destructor. Make it pure, provide an implementation. IF a
    >> non-virtual function is pure - how would you override it? And if you
    >> will not override it, what does it mean? Nothing. So, no use having
    >> such members.

    >
    > Yes, you are probably referring to current standards and
    > implementation of c++,
    > however, when the standard was being developed, some one could have
    > thought of pure ordinary member functions and a mechanism to override
    > them too... for some reason they haven't done that... which is what i
    > was wondering about...


    The difference between a virtual and non-virtual function is that
    virtual functions are designed to be overrideable, if you try to make a
    non-virtual function overrideable you will end up with a virtual function.

    There are three kinds of languages, those with no virtual functions
    (they usually do not have much OO support either), those with both
    virtual and non virtual functions (like C++) and those with only virtual
    functions (Java).

    --
    Erik Wikström
     
    Erik Wikström, Dec 18, 2007
    #6
  7. Rahul

    Rahul Guest

    On Dec 19, 12:00 am, Kira Yamato <> wrote:
    > On 2007-12-18 13:52:49 -0500, Rahul <> said:
    >
    > > Hi,

    >
    > > I tried to create a abstract class by having a non-virtual member
    > > function as pure, but i got a compilation error saying "only virtual
    > > member functions can be pure"...

    >
    > > I'm trying to think the reason behind this restriction... i just want
    > > to want a base class to be abstract so as to avoid object slicing into
    > > the base type from derived type, and in my case base class doesn't
    > > need to have a virtual function.

    >
    > I don't understand your goal. What do you mean by "object slicing into
    > the base type from derived type?"
    >
    >
    >
    > > Is there any alternative? and what is the reason behind only virtual
    > > member functions being pure?

    >
    > --
    >
    > -kira


    class A
    {
    };

    class B : public A
    {
    };

    fun(A obj)
    {
    }

    int main()
    {
    B obj;
    fun(obj); // Object of type B is upcasted into type of A and it
    is sliced. If i had a pure virtual member function in A, this could
    have been avoided, i would have basically got a compilation error...
    }
     
    Rahul, Dec 19, 2007
    #7
  8. On Dec 19, 8:31 am, Rahul <> wrote:
    > On Dec 19, 12:00 am, Kira Yamato <> wrote:
    >
    >
    >
    >
    >
    > > On 2007-12-18 13:52:49 -0500, Rahul <> said:

    >
    > > > Hi,

    >
    > > > I tried to create a abstract class by having a non-virtual member
    > > > function as pure, but i got a compilation error saying "only virtual
    > > > member functions can be pure"...

    >
    > > > I'm trying to think the reason behind this restriction... i just want
    > > > to want a base class to be abstract so as to avoid object slicing into
    > > > the base type from derived type, and in my case base class doesn't
    > > > need to have a virtual function.

    >
    > > I don't understand your goal. What do you mean by "object slicing into
    > > the base type from derived type?"

    >
    > > > Is there any alternative? and what is the reason behind only virtual
    > > > member functions being pure?

    >
    > > --

    >
    > > -kira

    >
    > class A
    > {
    >
    > };
    >
    > class B : public A
    > {
    >
    > };
    >
    > fun(A obj)
    > {
    >
    > }
    >
    > int main()
    > {
    > B obj;
    > fun(obj); // Object of type B is upcasted into type of A and it
    > is sliced. If i had a pure virtual member function in A, this could
    > have been avoided, i would have basically got a compilation error...


    You could simply mark the base copy constructor as protected member!
     
    Abhishek Padmanabh, Dec 19, 2007
    #8
  9. Rahul

    Kira Yamato Guest

    On 2007-12-18 22:31:02 -0500, Rahul <> said:

    > On Dec 19, 12:00 am, Kira Yamato <> wrote:
    >> On 2007-12-18 13:52:49 -0500, Rahul <> said:
    >>
    >>> Hi,

    >>
    >>> I tried to create a abstract class by having a non-virtual member
    >>> function as pure, but i got a compilation error saying "only virtual
    >>> member functions can be pure"...

    >>
    >>> I'm trying to think the reason behind this restriction... i just want
    >>> to want a base class to be abstract so as to avoid object slicing into
    >>> the base type from derived type, and in my case base class doesn't
    >>> need to have a virtual function.

    >>
    >> I don't understand your goal. What do you mean by "object slicing into
    >> the base type from derived type?"
    >>
    >>
    >>
    >>> Is there any alternative? and what is the reason behind only virtual
    >>> member functions being pure?

    >>
    >> --
    >>
    >> -kira

    >
    > class A
    > {
    > };
    >
    > class B : public A
    > {
    > };
    >
    > fun(A obj)
    > {
    > }


    True. It will avoid your case, but it won't avoid

    fun(A &obj)

    or

    func(A *obj)
    even if A is abstract.

    >
    > int main()
    > {
    > B obj;
    > fun(obj); // Object of type B is upcasted into type of A and it
    > is sliced. If i had a pure virtual member function in A, this could
    > have been avoided, i would have basically got a compilation error...
    > }


    So, if you want to prevent using a derived class as its base class, you
    can inherit as protected or private:

    class B : protected A

    or

    class B : private A.

    --

    -kira
     
    Kira Yamato, Dec 19, 2007
    #9
  10. Rahul

    James Kanze Guest

    On Dec 18, 7:52 pm, Rahul <> wrote:
    > I tried to create a abstract class by having a non-virtual member
    > function as pure, but i got a compilation error saying "only virtual
    > member functions can be pure"...


    > I'm trying to think the reason behind this restriction...


    Maybe the fact that making a non-virtual function pure has no
    sense. Making a function pure means that you must override it
    in a derived class. Making a function non-virtual means that
    the function cannot be overridden in a derived class. What
    would combining the two mean (other than making the class
    unusable)?

    > i just want to want a base class to be abstract so as to avoid
    > object slicing into the base type from derived type, and in my
    > case base class doesn't need to have a virtual function.


    If you are deriving from the class, it should either have a
    virtual destructor, or the semantics of the base class are such
    that it wouldn't occur to anyone to declare one (e.g. something
    like std::iterator). If there is reason to worry about slicing,
    you almost certainly need a virtual destructor as well.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Dec 19, 2007
    #10
  11. Rahul

    Pete Becker Guest

    On 2007-12-18 22:31:02 -0500, Rahul <> said:

    > On Dec 19, 12:00 am, Kira Yamato <> wrote:
    >> On 2007-12-18 13:52:49 -0500, Rahul <> said:
    >>
    >>
    >>> I'm trying to think the reason behind this restriction... i just want
    >>> to want a base class to be abstract so as to avoid object slicing into
    >>> the base type from derived type, and in my case base class doesn't
    >>> need to have a virtual function.

    >>

    >
    > fun(A obj)
    > {
    > }
    >
    > int main()
    > {
    > B obj;
    > fun(obj); // Object of type B is upcasted into type of A and it
    > is sliced. If i had a pure virtual member function in A, this could
    > have been avoided, i would have basically got a compilation error...
    > }


    If A was an abstract class, you wouldn't be able to call fun at all,
    because you wouldn't be able to create an A object for its argument.

    A obj; // illegal: A is abstract
    fun(obj); // can't get here

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
     
    Pete Becker, Dec 19, 2007
    #11
    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. Todd Aspeotis
    Replies:
    3
    Views:
    467
    Kanenas
    May 30, 2005
  2. John Goche
    Replies:
    10
    Views:
    753
    Marcus Kwok
    Dec 8, 2006
  3. Rahul
    Replies:
    3
    Views:
    348
    Salt_Peter
    Dec 2, 2007
  4. Hicham Mouline
    Replies:
    0
    Views:
    435
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    420
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page