'const' getting in the way

This is a discussion on 'const' getting in the way within the C++ Programming forums, part of the General Programming Boards category; this is some of my code, could you work out how i am suppose to do this? /*from main.cpp*/ Code: ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    'const' getting in the way

    this is some of my code, could you work out how i am suppose to do this?


    /*from main.cpp*/
    Code:
    student.read(cin);
    student.print(cout) << endl;
    /*from student.h*/
    Code:
    class Student {
     public:
            const char * getStudentID() const;
            const char * getStudentName() const;
            const char * getStudents() const;
            ostream& print(ostream& stream) const;
            istream& read(istream& stream) const;
     private:
            string StudentName;
            int StudentID;
    
    }

    /*from student.cpp*/

    Code:
    const char * Student::getStudents() const{
      string tmp;
      tmp = getStudentName();
      tmp += ": ";
      tmp += getStudentID();
    
      return (const char *) tmp.c_str();
    }
    
    ostream& Student::print(ostream& stream = cout) const {
       stream << getStudents() << endl;
       return stream;
    }
    
    istream& Student::read(istream& stream) const {
    
       /*What goes here? any help?
           getline(stream, name);  //student name
           getline(stream, id); //student id
           want to do..
           StudentName = name;
           StudentID = id;
           but this wont work..
       */
    
           return stream;
    }
    ---------------------------------------------------

    im not sure how to write the read function. Could anyone give me a hand?
    I would like the read function to read in the student's details (the Student's Name, and ID) and then call the function getStudents();

    The main problem im having is to do with the const. It wont let me set the variables i wish to set.
    thanks

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Your read function should not be const. It changes the data in object that it is called from. Only functions that don't change the logical state of the object should be made const.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    thanks...

    i gotta engrave that into my brain or something, cause i keep forgetting that const means you cant set things..

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Does this mean: "const char * getStudentID() const;" that the function returns a constant pointer to a character constant?

    Like const char *const cTemp. The syntax just looks a bit strange haveing a const after the function name. I would of thought it would of been this: const char *const getStudentID().

    Anyone.....?
    Be a leader and not a follower.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by subdene
    Does this mean: "const char * getStudentID() const;" that the function returns a constant pointer to a character constant?
    No, the last const means that the function is constant, ie not able to modify any variables within the class.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    why don't you just overload the insertion operators?

    Code:
    class ClassName
    {
    	friend ostream& operator<<(ostream&,const ClassName);
    public: 
    	ClassName(int ini) : i(ini) {} ;
    private:
    	int i;
    };
    
    ostream& operator<<(ostream& out,const ClassName C)
    {
    	return out<<C.i;
    }
    that way you can use a constant if you wish.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    Quote Originally Posted by major_small
    why don't you just overload the insertion operators?

    Code:
    class ClassName
    {
    	friend ostream& operator<<(ostream&,const ClassName);
    public: 
    	ClassName(int ini) : i(ini) {} ;
    private:
    	int i;
    };
    
    ostream& operator<<(ostream& out,const ClassName C)
    {
    	return out<<C.i;
    }
    that way you can use a constant if you wish.
    you gave extraction as an example, and when you use inserrtion you still change values so i dont think it could be const anyways
    Last edited by ryan_germain; 07-30-2004 at 09:36 AM.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by ryan_germain
    wouldn't you use istream with the insertion operator instead? and ostream with extraction operator?
    istream is for input, and is a stream where you can extract things from, whereas ostream is a stream for output, and you would insert things onto it to eventually go to an output device...

    I would tell you to do a google search on 'extraction operators', but the first result it turns up is about.com, which happens to have the wrong answer...

    This is why I don't use about.com
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Regardless, when using operator>> with an istream, if it is a member function it should not be const, and if it is a global function it should not take a const reference as the second argument. It's the same principle as if it was a read function.

    The operator<< on the other hand should generally be const if it is a member, and should take a const reference if it is a global function - just like the print function in the OP's code is allowed to be const.

  10. #10
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    Quote Originally Posted by major_small
    I would tell you to do a google search on 'extraction operators', but the first result it turns up is about.com, which happens to have the wrong answer...

    This is why I don't use about.com
    euh i always thought << was the extraction operator as in you extract it from from the variable and into the console... ...obviously wrong...so sorry bout the mix up major small

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You can cast away const using const_cast. However in your situation using it would be abusing the feature.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21