HP3000-L Archives

December 2004, Week 3

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Keven Miller <[log in to unmask]>
Reply To:
Keven Miller <[log in to unmask]>
Date:
Fri, 17 Dec 2004 16:55:44 -0500
Content-Type:
text/plain
Parts/Attachments:
text/plain (117 lines)
More info.
I understand the basic idea of overloading where I can have
a procedure name used more than once, each having a different
number and/or types of parameters, and possibly different
return types.

But in this example, both procedures are type bool,
they both have void or no parameters,
and they only return a value with no other variables
affected.

I did get this responce from another source; but its
in C++ "jiberish" that I'm still trying to digest.
=============================================================
From "Effective C++":

<quote>

The purpose of const member functions, of course, is to specify which member
functions may be invoked on const objects.  Many people overlook the fact
that member functions differing only in their const-ness can be overloaded,
however, this is an important feature of C++.  Consider the String class
once again:

class String {
public:
        ...
        // operator[] for non-const objects
        char& operator[](int position)
                { return data[ position ];}
        // operator[] for const objects
        const char& operator[](int position) const
                { return data[ position ];}

private:
        char *data;
};

String s1 = "Hello";
cout << s1[0];          // calls non-const String::operator[]

const String s2 = "World";
cout << s2[0];          // calls const String::operator[]

By overloading operator[] and giving the different versions different return
values, you are able to have const and non-const Strings handled
differently:

String s = "Hello";     // non-const String object

cout << s[0];           // fine - reading a non-const String

s[0] = 'x';                     // fine - writing a non-const String

const String cs = "World";      // const String object

cout << cs[0];          // fine - reading a const String

cs[0] = 'x';            // error! - writing a const String

By the way, note that the error here has only to do with the return value of
the operator[] that is called; the calls to operator[] themselves are all
fine.  The error arises out of an attempt to make an assignment to a const
char&, because that's the return value from the const version of operator[].

</quote>

I hope this helps, essentially it looks like in your class, that if someone
creates a const of your class "name", then the isOpen() const; will be
called.  If a non-const object of class "name" is created, then isOpen(); is
called instead.
=============================================================
_______________________________________________
Keven Miller       [log in to unmask]


-----Original Message-----
From: HP-3000 Systems Discussion [mailto:[log in to unmask]]On
Behalf Of Keven Miller
Sent: Friday, December 17, 2004 12:31 PM
To: [log in to unmask]
Subject: [HP3000-L] C++ class help


I figure I'm strong in C, but this C++ sometimes leaves
me baffled.

I'm looking at a class definition with a method
declared twice. With the only difference, the word const.
Can you tell me why or whats going on and why I would do this?


class name:public other {
public:
        virtual bool isOpen() const;
        virtual bool isOpen (void);
protected:
        bool localOpen;
};

bool name::isOpen (void) const {
        return localOpen;
}

bool name::isOpen (void) {
        return localOpen;
}

_______________________________________________
Keven Miller       [log in to unmask]

* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *

* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *

ATOM RSS1 RSS2