private destructor in base class

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

  1. Rahul

    Rahul Guest

    Hi Everyone,

    I was trying to implement a final class and i start having the
    destructor of the final class as private,

    class A
    {
    ~A()
    {
    printf("destructor invoked\n");
    }
    };


    class B : public A
    {
    public : ~B()
    {
    }
    };

    int main()
    {
    return 0;
    }

    And as expected i get a compilation error, saying "cannot access
    private member A::~A declared in A"

    But when i change the derived class like the following,

    class B : public A
    {
    }

    int main()
    {
    return (0);
    }

    there is no compilation error, i get an error only when i try to
    create an object of the class B. In other words, class B is just an
    abstract class. Doesn't the default destructor in B tries to access
    the destructor of A and shouldn't that give a compilation error in the
    first place, similar to the case where the user provides the
    destructor in B?
     
    Rahul, Dec 22, 2007
    #1
    1. Advertising

  2. Rahul

    Guest

    On Dec 22, 1:49 pm, Rahul <> wrote:
    > Hi Everyone,
    >
    >  I was trying to implement a final class and i start having the
    > destructor of the final class as private,
    >
    > class A
    > {
    >        ~A()
    >                 {
    >                         printf("destructor invoked\n");
    >                 }
    >
    > };
    >
    > class B : public A
    > {
    > public : ~B()
    >                  {
    >                  }
    >
    > };
    >
    > int main()
    > {
    >   return 0;
    >
    > }
    >
    > And as expected i get a compilation error, saying "cannot access
    > private member A::~A declared in A"
    >
    > But when i change the derived class like the following,
    >
    > class B : public A
    > {
    >
    > }
    >
    > int main()
    > {
    >  return (0);
    >
    > }
    >
    > there is no compilation error, i get an error only when i try to
    > create an object of the class B. In other words, class B is just an
    > abstract class. Doesn't the default destructor in B tries to access
    > the destructor of A and shouldn't that give a compilation error in the
    > first place, similar to the case where the user provides the
    > destructor in B?


    hi rahul,

    Class B in second case didn't have any ctor or dtor method defined, so
    compiler doesn't check class A dctor being private.if you define a
    ctor/dtor for class B . compiler will throw an error.
     
    , Dec 22, 2007
    #2
    1. Advertising

  3. Rahul

    Rahul Guest

    On Dec 22, 5:56 pm, wrote:
    > On Dec 22, 1:49 pm, Rahul <> wrote:
    >
    >
    >
    > > Hi Everyone,

    >
    > > I was trying to implement a final class and i start having the
    > > destructor of the final class as private,

    >
    > > class A
    > > {
    > > ~A()
    > > {
    > > printf("destructor invoked\n");
    > > }

    >
    > > };

    >
    > > class B : public A
    > > {
    > > public : ~B()
    > > {
    > > }

    >
    > > };

    >
    > > int main()
    > > {
    > > return 0;

    >
    > > }

    >
    > > And as expected i get a compilation error, saying "cannot access
    > > private member A::~A declared in A"

    >
    > > But when i change the derived class like the following,

    >
    > > class B : public A
    > > {

    >
    > > }

    >
    > > int main()
    > > {
    > > return (0);

    >
    > > }

    >
    > > there is no compilation error, i get an error only when i try to
    > > create an object of the class B. In other words, class B is just an
    > > abstract class. Doesn't the default destructor in B tries to access
    > > the destructor of A and shouldn't that give a compilation error in the
    > > first place, similar to the case where the user provides the
    > > destructor in B?

    >
    > hi rahul,
    >
    > Class B in second case didn't have any ctor or dtor method defined, so
    > compiler doesn't check class A dctor being private.if you define a
    > ctor/dtor for class B . compiler will throw an error.


    Yes i understand that, but is there anyway so that i get a compilation
    error for second case? i mean, without defining a constructor (or)
    destructor in the derived class...
     
    Rahul, Dec 22, 2007
    #3
  4. On Dec 22, 10:03 pm, Rahul <> wrote:
    > On Dec 22, 5:56 pm, wrote:
    > > On Dec 22, 1:49 pm, Rahul <> wrote:

    >
    > > > Hi Everyone,

    >
    > > >  I was trying to implement a final class and i start having the
    > > > destructor of the final class as private,

    >
    > > > class A
    > > > {
    > > >        ~A()
    > > >                 {
    > > >                         printf("destructor invoked\n");
    > > >                 }

    >
    > > > };

    >
    > > > class B : public A
    > > > {
    > > > public : ~B()
    > > >                  {
    > > >                  }

    >
    > > > };

    >
    > > > int main()
    > > > {
    > > >   return 0;

    >
    > > > }

    >
    > > > And as expected i get a compilation error, saying "cannot access
    > > > private member A::~A declared in A"

    >
    > > > But when i change the derived class like the following,

    >
    > > > class B : public A
    > > > {

    >
    > > > }

    >
    > > > int main()
    > > > {
    > > >  return (0);

    >
    > > > }

    >
    > > > there is no compilation error, i get an error only when i try to
    > > > create an object of the class B. In other words, class B is just an
    > > > abstract class. Doesn't the default destructor in B tries to access
    > > > the destructor of A and shouldn't that give a compilation error in the
    > > > first place, similar to the case where the user provides the
    > > > destructor in B?



    Note that the default methods of a class that a compiler can generate
    namely: default constructor, destructor, assignment operator, copy
    constructor, pair of address-of operators (did I miss anything?), are
    only provided by compilers when they are needed (or your code shows a
    need of them).

    If you explicitly write a destructor, it shows a compilation error.
    But if you do not explicitly write it, and you do not create an object
    of a class none of the above would be needed and hence compiler does
    not generate them. And if the compiler does not generate them, there
    is no compilation around them and hence no errors. Those default
    members are only generated when they are *needed*. I am not sure if
    the standard mandates that or if it is a general compiler
    implementation strategy.

    >
    > > hi rahul,

    >
    > > Class B in second case didn't have any ctor or dtor method defined, so
    > > compiler doesn't check class A dctor being private.if you define a
    > > ctor/dtor for class B . compiler will throw an error.

    >
    > Yes i understand that, but is there anyway so that i get a compilation
    > error for second case? i mean, without defining a constructor (or)
    > destructor in the derived class


    I am not at all sure what you ask for here. What do you mean "is there
    anyway so that i get a compilation error for second case"? Create an
    object, you will get an error. Sorry, I am not getting the context of
    your question. Why would you need that and how would that be helpful
    to you.
     
    Abhishek Padmanabh, Dec 22, 2007
    #4
  5. In article
    <>,
    Abhishek Padmanabh <> wrote:

    > On Dec 22, 10:03 pm, Rahul <> wrote:


    > I am not at all sure what you ask for here. What do you mean "is there
    > anyway so that i get a compilation error for second case"? Create an
    > object, you will get an error. Sorry, I am not getting the context of
    > your question. Why would you need that and how would that be helpful
    > to you.


    I guessing Rahul wants to catch a /design/ error before the object is
    used, by causing the compiler to generate an error message to flag the
    undesired inheritance.

    Unfortunately it's very difficult to implement "final" class semantics
    in C++. Why would you need it anyway?

    -dr
     
    Dave Rahardja, Dec 23, 2007
    #5
  6. Rahul

    Rahul Guest

    On Dec 22, 10:26 pm, Abhishek Padmanabh <>
    wrote:
    > On Dec 22, 10:03 pm, Rahul <> wrote:
    >
    >
    >
    > > On Dec 22, 5:56 pm, wrote:
    > > > On Dec 22, 1:49 pm, Rahul <> wrote:

    >
    > > > > Hi Everyone,

    >
    > > > > I was trying to implement a final class and i start having the
    > > > > destructor of the final class as private,

    >
    > > > > class A
    > > > > {
    > > > > ~A()
    > > > > {
    > > > > printf("destructor invoked\n");
    > > > > }

    >
    > > > > };

    >
    > > > > class B : public A
    > > > > {
    > > > > public : ~B()
    > > > > {
    > > > > }

    >
    > > > > };

    >
    > > > > int main()
    > > > > {
    > > > > return 0;

    >
    > > > > }

    >
    > > > > And as expected i get a compilation error, saying "cannot access
    > > > > private member A::~A declared in A"

    >
    > > > > But when i change the derived class like the following,

    >
    > > > > class B : public A
    > > > > {

    >
    > > > > }

    >
    > > > > int main()
    > > > > {
    > > > > return (0);

    >
    > > > > }

    >
    > > > > there is no compilation error, i get an error only when i try to
    > > > > create an object of the class B. In other words, class B is just an
    > > > > abstract class. Doesn't the default destructor in B tries to access
    > > > > the destructor of A and shouldn't that give a compilation error in the
    > > > > first place, similar to the case where the user provides the
    > > > > destructor in B?

    >
    > Note that the default methods of a class that a compiler can generate
    > namely: default constructor, destructor, assignment operator, copy
    > constructor, pair of address-of operators (did I miss anything?), are
    > only provided by compilers when they are needed (or your code shows a
    > need of them).
    >
    > If you explicitly write a destructor, it shows a compilation error.
    > But if you do not explicitly write it, and you do not create an object
    > of a class none of the above would be needed and hence compiler does
    > not generate them. And if the compiler does not generate them, there
    > is no compilation around them and hence no errors. Those default
    > members are only generated when they are *needed*. I am not sure if
    > the standard mandates that or if it is a general compiler
    > implementation strategy.
    >
    >
    >
    > > > hi rahul,

    >
    > > > Class B in second case didn't have any ctor or dtor method defined, so
    > > > compiler doesn't check class A dctor being private.if you define a
    > > > ctor/dtor for class B . compiler will throw an error.

    >
    > > Yes i understand that, but is there anyway so that i get a compilation
    > > error for second case? i mean, without defining a constructor (or)
    > > destructor in the derived class

    >
    > I am not at all sure what you ask for here. What do you mean "is there
    > anyway so that i get a compilation error for second case"? Create an
    > object, you will get an error. Sorry, I am not getting the context of
    > your question. Why would you need that and how would that be helpful
    > to you.


    Well, i just want to implement a final class... and i would like the
    compiler to give an error as soon as someone tries to derive from it
    instead of waiting for an object to be created. Currently, as you
    pointed out, there is no error until someone creates an object.
    In all those cases, the developer of the new class (derived from my
    final class) can still have some static member functions of the class
    and can use the same...

    I basically want to stop someone from doing so... why so? thats
    altogether another ball game...
     
    Rahul, Dec 23, 2007
    #6
  7. On 2007-12-23 13:52, Rahul wrote:
    > On Dec 22, 10:26 pm, Abhishek Padmanabh <>
    > wrote:
    >> On Dec 22, 10:03 pm, Rahul <> wrote:
    >>
    >>
    >>
    >> > On Dec 22, 5:56 pm, wrote:
    >> > > On Dec 22, 1:49 pm, Rahul <> wrote:

    >>
    >> > > > Hi Everyone,

    >>
    >> > > > I was trying to implement a final class and i start having the
    >> > > > destructor of the final class as private,

    >>
    >> > > > class A
    >> > > > {
    >> > > > ~A()
    >> > > > {
    >> > > > printf("destructor invoked\n");
    >> > > > }

    >>
    >> > > > };

    >>
    >> > > > class B : public A
    >> > > > {
    >> > > > public : ~B()
    >> > > > {
    >> > > > }

    >>
    >> > > > };

    >>
    >> > > > int main()
    >> > > > {
    >> > > > return 0;

    >>
    >> > > > }

    >>
    >> > > > And as expected i get a compilation error, saying "cannot access
    >> > > > private member A::~A declared in A"

    >>
    >> > > > But when i change the derived class like the following,

    >>
    >> > > > class B : public A
    >> > > > {

    >>
    >> > > > }

    >>
    >> > > > int main()
    >> > > > {
    >> > > > return (0);

    >>
    >> > > > }

    >>
    >> > > > there is no compilation error, i get an error only when i try to
    >> > > > create an object of the class B. In other words, class B is just an
    >> > > > abstract class. Doesn't the default destructor in B tries to access
    >> > > > the destructor of A and shouldn't that give a compilation error in the
    >> > > > first place, similar to the case where the user provides the
    >> > > > destructor in B?

    >>
    >> Note that the default methods of a class that a compiler can generate
    >> namely: default constructor, destructor, assignment operator, copy
    >> constructor, pair of address-of operators (did I miss anything?), are
    >> only provided by compilers when they are needed (or your code shows a
    >> need of them).
    >>
    >> If you explicitly write a destructor, it shows a compilation error.
    >> But if you do not explicitly write it, and you do not create an object
    >> of a class none of the above would be needed and hence compiler does
    >> not generate them. And if the compiler does not generate them, there
    >> is no compilation around them and hence no errors. Those default
    >> members are only generated when they are *needed*. I am not sure if
    >> the standard mandates that or if it is a general compiler
    >> implementation strategy.
    >>
    >>
    >>
    >> > > hi rahul,

    >>
    >> > > Class B in second case didn't have any ctor or dtor method defined, so
    >> > > compiler doesn't check class A dctor being private.if you define a
    >> > > ctor/dtor for class B . compiler will throw an error.

    >>
    >> > Yes i understand that, but is there anyway so that i get a compilation
    >> > error for second case? i mean, without defining a constructor (or)
    >> > destructor in the derived class

    >>
    >> I am not at all sure what you ask for here. What do you mean "is there
    >> anyway so that i get a compilation error for second case"? Create an
    >> object, you will get an error. Sorry, I am not getting the context of
    >> your question. Why would you need that and how would that be helpful
    >> to you.

    >
    > Well, i just want to implement a final class... and i would like the
    > compiler to give an error as soon as someone tries to derive from it
    > instead of waiting for an object to be created. Currently, as you
    > pointed out, there is no error until someone creates an object.
    > In all those cases, the developer of the new class (derived from my
    > final class) can still have some static member functions of the class
    > and can use the same...
    >
    > I basically want to stop someone from doing so... why so? thats
    > altogether another ball game...


    Use Java or C#. None have come up with a good reason to include a final
    function in C++ so there is none, but there are a few ways to emulate it
    but none of them are perfect. The FAQ list three ways of doing it:
    http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.11

    --
    Erik Wikström
     
    Erik Wikström, Dec 23, 2007
    #7
  8. On Sat, 22 Dec 2007 00:49:25 -0800, Rahul wrote:

    > Hi Everyone,
    >
    > I was trying to implement a final class and i start having the
    > destructor of the final class as private,
    >
    > class A
    > {
    > ~A()
    > {
    > printf("destructor invoked\n");
    > }
    > };


    Note that such construct causes also problems with destructing objects of
    class A, not only of classes derived from A.

    --
    Tadeusz B. Kopec ()
    A light wife doth make a heavy husband.
    -- Wm. Shakespeare, "The Merchant of Venice"
     
    Tadeusz B. Kopec, Dec 27, 2007
    #8
    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. qazmlp
    Replies:
    1
    Views:
    585
    qazmlp
    Apr 10, 2005
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    564
    John Carson
    Sep 3, 2005
  3. frs
    Replies:
    20
    Views:
    783
    Alf P. Steinbach
    Sep 21, 2005
  4. arun
    Replies:
    2
    Views:
    565
    benben
    Jun 13, 2006
  5. Replies:
    4
    Views:
    489
    Zeppe
    Sep 12, 2008
Loading...

Share This Page