Thread: Public: ~Class() is allowed?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    17

    Question Public: ~Class() is allowed?

    Is the following code allowed:

    Code:
    public: ~Class()
    {
        blahblah
    }
    I coded it inside one of my class and I executed it in my startup program. It works perfectly.

    From what Ive read from a book, it says class destructor forbids any kind of public in front of the tilde ~.

    Can someone explain? Thanks in advance

    Edit: I changed the "P" to small "p". Ive some typo error back then.
    Last edited by Lincoln_Poh; 09-26-2008 at 08:23 PM.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    That shouldn't compile since C++ is case sensitive.
    But this:
    Code:
    class Class
    {
    public: ~Class() {}
    };
    is the same as this:
    Code:
    class Class
    {
    public:
       ~Class() {}
    };
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    17
    Quote Originally Posted by cpjust View Post
    That shouldn't compile since C++ is case sensitive.
    But this:
    Code:
    class Class
    {
    public: ~Class() {}
    };
    is the same as this:
    Code:
    class Class
    {
    public:
       ~Class() {}
    };
    Nono, cpjust, my main problem is to ask, what's the difference between:

    Code:
    public: ~class()
    and
    Code:
     
    ~class()
    I want to know does C++ allow us to preface public or any kind of other words in front of the tilde ~class ?

    For my case here, I have coded it inside my startup program, and it ran perfectly. I want to know why does it ran perfectly. The book which Im reading said that C++ forbids public: in front of the class destructor...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In this case, Public is just a label (that you could use with goto), not a namespace or a class or an actual public declaration or anything like that. (I wouldn't think you could label a function, but if you say it works who am I to say no.) I would think this would have to be inside the class declaration for ~Class by itself to make sense. EDIT: And with a small p, none of this part matters! Yay!

    Even if it was lowercase, I don't see what the problem is, exactly. Wouldn't a destructor, if you defined it, have to be public to work?
    Last edited by tabstop; 09-26-2008 at 08:38 PM.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Did you create a default constructor? If not the compiler might be implying a default constructor before ~Class(); so then it would be compiling correctly and not have any run time errors. This is just my wild guess, though, so don't take it as truth.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Lincoln_Poh View Post
    Nono, cpjust, my main problem is to ask, what's the difference between:

    Code:
    public: ~class()
    and
    Code:
     
    ~class()
    I want to know does C++ allow us to preface public or any kind of other words in front of the tilde ~class ?

    For my case here, I have coded it inside my startup program, and it ran perfectly. I want to know why does it ran perfectly. The book which Im reading said that C++ forbids public: in front of the class destructor...
    Putting public: infront of or above makes no difference, as long as there is some whitespace between them.

    If you have this:
    Code:
    class Class
    {
       ~Class();
    };
    Then your destructor would be private, the same as this:
    Code:
    class Class
    {
    private:
       ~Class();
    };
    and if your destructor is private, you cannot instantiate that class.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by cpjust View Post
    if your destructor is private, you cannot instantiate that class.
    You could if it's a singleton.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    17
    Omg, everyone here explain things until so profound. Can someone simplify things so its easier for me to at least understand 80% of it >.<?

    My question again is: Does C++ allow us to put "public:" in front of a class destructor? Ive no idea what in the world is a default. My problem is destructor, not contructor...

    Thanks in advance...

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by King Mir View Post
    You could if it's a singleton.
    Well sure, you can create a pointer to it, but not a regular stack variable.
    It doesn't even need to be a singleton, as long as you provide a Delete() method:
    Code:
    class Test
    {
       ~Test() {}
    
    public:
       void Delete() { delete this; }
    };
    
    int main()
    {
       Test* pT = new Test;
    
       pT->Delete();
       return 0;
    }
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Lincoln_Poh View Post
    Omg, everyone here explain things until so profound. Can someone simplify things so its easier for me to at least understand 80% of it >.<?

    My question again is: Does C++ allow us to put "public:" in front of a class destructor? Ive no idea what in the world is a default. My problem is destructor, not contructor...

    Thanks in advance...
    Yes, you can put public: or protected: or private: anywhere you want inside a struct or class. There's nothing magical about them.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems that in C# every method must have a modifier (private/public), except destructors which are called by the garbage collector.

    This doesn't apply in C++. The modifiers need not be added to every method, the private or public keyword applies to all methods that follow it until a different modifier appears or the end of the class declaration.

    In C++, normally destructors are public, since otherwise you can't create stack instances, or call delete on a dynamically allocated instance. But you may sometimes make it private to control who can destroy instances.

    However, destructors are practically never called explicitly by you. Even though they are public, you must never do the following:

    Code:
    #include <iostream>
    
    class X
    {
        public:
        ~X() {}
    };
    
    int main()
    {
        X x;
        x.~X();  //very wrong
    }
    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).

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by Lincoln_Poh View Post
    Ive no idea what in the world is a default.
    A default constructor looks like this

    Code:
    class foo
    {
         private:
              int data;
         public:
              foo(); //THIS IS A DEFAULT CONSTRUCTOR
              foo(int, int); //another constructor, but with arguments
              ~foo(); //destructor
    };
    You can have multiple constructors, where each one accepts a different argument. A default one basically means if the person initializes the class without passing it any arguments then it will do something (usually set int "data" to something) by default.
    Code:
    foo fooClass; //uses the default constructor to set the int "data"
    foo fooClass2(5,10); //uses the other constructor to set other things.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    17
    Thanks all. I assume the book that told me that a no keywords, such as public or private, in front of a class cestructor, is false. lol...this is quite some book I have right here..

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your book might have been referring to java, where you can put public or private in front of every member function (and member variable):
    Code:
    public class MyClass
    {
      public MyClass() { }
    }
    In C++, the access specifier is like a label with a colon after it, and any member declarations after it belong to that access level (until a different access level is given). It doesn't matter if there is a newline after the colon or not, since the C++ grammar doesn't care. So technically you can have a keyword like public in front of and on the same line as the constructor or destructor, but it has to have a colon between them. My guess is that the book is saying you can't do it like Java and have public in front without the colon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Process Memory
    By polydegmon in forum C# Programming
    Replies: 0
    Last Post: 05-26-2009, 07:18 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 02:39 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM