Thread: C++ question

  1. #16
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Are you trying to say that a destructor isn't a function because it doesn't return anything?

    I don't have a book with me, but I'm pretty sure that Stroustrup refers to constructors and destructors as functions despite the fact that they don't have return values.

    EDIT: Yup, they're still called member functions -- from Stroustrup
    Last edited by Polymorphic OOP; 12-20-2002 at 12:10 PM.

  2. #17
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Destructors can't take arguments, can't be overloaded, and don't return anything. I wouldn't call them a function.

  3. #18
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>Because pure virtual destructors must have a definition/implementation, so "C" isn't really correct.

    The above is incorrect. Below is an example of a pure virtual function. Answer C is the correct answer to the question.

    A virtual function is made pure by initializing it with zero, as in

    virtual void Draw() = 0;
    _________________________________

    As to problems: if you've read the texts indicated you are ready to try things out for yourself. As you run into stumbling blocks go back and reread the texts or post pertinent questions/code to a helpful board such as this one. Here's some examples:

    Write code for your favorite card game.

    Write code for a version of battleship.

    Write code that determines the segments of overlap if you are given a line segment and a simple concave polygon which are coplanar and restricted to a given plane. Point contacts are ignored but overlapping coincident line segments count.

    Whatever else tickles your fancy.

  4. #19
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Note update on previous post

  5. #20
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by elad
    >>Because pure virtual destructors must have a definition/implementation, so "C" isn't really correct.

    The above is incorrect. Below is an example of a pure virtual function. Answer C is the correct answer to the question.

    A virtual function is made pure by initializing it with zero, as in

    virtual void Draw() = 0;
    Then you've never used a pure virtual destructor

    A pure virtual destructor uses the = 0 notation and makes it so you can't create objects from the class (it makes the class abstract) however you still must provide a definition for the function.

    Look it up.

  6. #21
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Polymorphic OOP
    Are you trying to say that a destructor isn't a function because it doesn't return anything?

    I don't have a book with me, but I'm pretty sure that Stroustrup refers to constructors and destructors as functions despite the fact that they don't have return values.

    EDIT: Yup, they're still called member functions -- from Stroustrup
    Why not read the definition of a destructor? http://www.research.att.com/~bs/glos...ml#Gdestructor

  7. #22
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    because it doesn't say there whether or not a destructor is considered a function, however, previously your claim was that it wasn't a function because it had no return type yet Stroustrup specifically calls constructors functions. I posted the response prior to you bringing up the fact of them not being able to be overloaded, which is a good point, though I don't think it makes them not functions. They're just functions which can't be overloaded. If anyone has a book by Stroustrup handy then we'd be able to get a better answer.

    They can be explicitly called, they can be virtual, they can be defined by the user.

  8. #23
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    After further digging:

    From Liberty Teach Yourself C++ in 21 days:

    Abstract Data Types
    Declare a class to be an abstract data type by including one or more pure virtual functions in the class declaration. Declare a pure virtual function by writing = 0 after the function declaration. Example:

    class Shape
    {
    virtual void Draw() = 0; // pure virtual
    };

    Implementing Pure Virtual Functions
    Typically, the pure virtual functions in an abstract base class are never implemented. Because no objects of that type are ever created, there is no reason to provide implementations, and the ADT works purely as the definition of an interface to objects which derive from it.

    It is possible, however, to provide an implementation to a pure virtual function. ________________________________________

    I therefore concede the point to Polymorphic OOP.

  9. #24
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    because it doesn't say there whether or not a destructor is considered a function
    Exactly, they're destructors are referred to as class members.

    however, previously your claim was that it wasn't a function because it had no return type yet Stroustrup specifically calls constructors functions
    Read into it all you want, I simply asked what data type a destructor returned. I never said a destructor wasn't a function, based souly on the fact that it doesn't return a value.

    They're just functions which can't be overloaded. If anyone has a book by Stroustrup handy then we'd be able to get a better answer.
    Lets look at the definition of a function:
    function - a named sequence of statements that can be invoked/called given arguments and that might return a value. The type of the function includes the number and types of argument and the type of the value returned, if any. See also: function declaration, function body. TC++PL 2.3, 7, D&E 2.6.

  10. #25
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Eibro
    Exactly, they're destructors are referred to as class members.
    That doesn't mean it's not a member function. He just doesn't go on to say anything else.

    Originally posted by Eibro
    Read into it all you want, I simply asked what data type a destructor returned. I never said a destructor wasn't a function, based souly on the fact that it doesn't return a value.
    Well then what was your point in the statement. That's the only point I can think of which would have any relevance to the prior post.

    Originally posted by Eibro
    Lets look at the definition of a function:
    A destructor fits that definition.

  11. #26
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    C++ Standard 12.4.7:
    A destructor can be declared virtual (10.3) or pure virtual (10.4); if any objects of that class or any derived class are created in the program, the destructor shall be defined. If a class has a base class with a virtual destructor, its destructor (whether user or implicitly declared) is virtual.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #27
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    That doesn't mean it's not a member function. He just doesn't go on to say anything else.
    And because you saw the word "function" in the definition of a constructor doesn't mean a destructor is a function.

    Well then what was your point in the statement. That's the only point I can think of which would have any relevance to the prior post.
    I asked you a question, I didn't make a statement. You wanted to get picky about things, not me.

    A destructor fits that definition.
    No, it doesn't.

  13. #28
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Find the error(s) in each of the following and explain how to correct it (them).
    a) Assume the following prototype is declared in class Time:
    Code:
    void ~Time( int )
    b) The following is a partial definition of class Time.
    Code:
    class Time {
    public:
       // function prototypes
    private:
       int hour = 0;
       int minute = 0;
       int second = 0;
    };
    c) Assume the following prototype is declared in class Employee.
    Code:
    int Employee( const char *, const char * );

    In what context might the name operator/ be used in C++?


    How does the precedence of an overloaded operator in C++ compare with the precedence of the original operator?


    Name the 5 C++ operators that cannot be overloaded.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  14. #29
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Eibro
    And because you saw the word "function" in the definition of a constructor doesn't mean a destructor is a function.
    I posted that in the response to you apparently claiming that it wasn't a function because it had no return value. A constructor has no return value but it's a function so I posted that as proof that not all functions have a return type. No, it doesn't specifically say there that a destructor is a function but it contradicted your prior post which implied that functions needed a return type (though now you are apparently saying that that is not what you meant).

    Originally posted by Eibro
    I asked you a question, I didn't make a statement. You wanted to get picky about things, not me.
    So you're saying that you made a post which had absolutely no relevance what-so-ever to the topic? I see


    Originally posted by Eibro
    No, it doesn't.
    Yes it does -- it fits the definition completely.

  15. #30
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Nowhere in the C++ Standard does it say that destructors are member functions, but:

    "The invocation of a destructor is subject to the usual rules for member functions"

    IMO, they are functions, a special kind of functions.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM