trouble with const correctnes

This is a discussion on trouble with const correctnes within the C++ Programming forums, part of the General Programming Boards category; Hello all, i use the following functions in a class: Code: // .h ... private: string* machineId; ... public: const ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    trouble with const correctnes

    Hello all,

    i use the following functions in a class:

    Code:
    // .h
    ...
    private:
    string* machineId;
    ...
    public:
    const string* const getId() const {return machineId;}
    const string* getDebugString();
    ...
    
    // .cpp
    ...
    const string* Machine::getDebugString()
    {
    	stringstream ss;
    	ss << "    <machine>  id= " << *machineId << "  start state= " 
    		<< *( getStateFromStateId(startStateId)->getId() ) << "\n"; // *ERROR*
    	for (vector<State*>::const_iterator i = stateVector.begin(); i != stateVector.end(); ++i)
    	{
    		ss << *( (**i).getDebugString() );
    	}
    	ss << "    </machine>\n";
    	return new string( ss.str() );
    }
    ...
    The errormessage is:
    ./src/Machine.cpp: In member function 'const std::string* Machine::getDebugString()':
    ../src/Machine.cpp:23: error: passing 'const State' as 'this' argument of 'const std::string* State::getId()' discards qualifiers
    make: *** [src/Machine.o] Error 1
    Could someone please tell me what i'm doing wrong?
    I tried to apply const-correctness to my program. I guarantee the "getId() const" does not mutate anything in my object to the compiler but he still complains.

    thank you in advance!

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    How is getStateFromStateId() prototyped? My guess is that you are returning a copy of state, not a reference
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Hi mario,

    from same .h file:

    Code:
    const State* getStateFromStateId(string* sId) const;
    does this help?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    It didn't actually. My initial statement makes little sense anyways. Just too tired today.

    Please take a good look at the error message again... look at the prototype specified for stateID()

    const std::string* State::getId()
    There is no const there declaring the function as const.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by Mario F.
    It didn't actually. My initial statement makes little sense anyways. Just too tired today.

    Please take a good look at the error message again... look at the prototype specified for stateID()



    There is no const there declaring the function as const.
    Thank you, but I think I didn't get the message :-(
    in my initial post there is
    Code:
    // .h
    ...
    private:
    string* machineId;
    ...
    public:
    const string* const getId() const {return machineId;}  // <--prototype with const
    const string* getDebugString();
    ...
    so why do you say thte getId() method isn't const?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That is from the Machine class, right? Mario F. is referring to the State class's getId() function.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    sorry sorry sorry, you are completely right.
    It seems you know my code better than me, I better go hiding myself somethere

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 02:46 AM
  3. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM

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