Sign in to follow this  
thedustbustr

to const or not to const

Recommended Posts

thedustbustr    191
namespace dust {
template<class T> class node { /* ... */ };
template<class T>
std::ostream& operator <<(std::ostream &os,const node<T>& obj)
{
    os << obj.get();
	return os;
}
}; //namespace dust

//driver program
node<int>* t;
/* ... */
std::cout << *t;
error C2662: 'dust::node<T>::get' : cannot convert 'this' pointer from 'const 
dust::node<T>' to 'dust::node<T> &'
        with
        [
            T=int
        ]
        and
        [
            T=int
        ]
        and
        [
            T=int
        ]
My understanding of constness is very vague. Operator << should have const arguments because stream insertion does not require writing to the arguments. I don't understand why it matters if what is passed into the function also needs to be const. I should be able to pass in const and non-const arguments without problem, the only reason the argument is const in operator<< is because it doesn't need to modify them. What am I doing wrong? I've been told that needing const_cast<> indicates a design flaw.

Share this post


Link to post
Share on other sites
Trap    684
Suppose you do this:
const Thing b;
b.method();

Then it will only compile if method is declared const like this:
void method() const {
// stuff
}

Share this post


Link to post
Share on other sites
eyal    192
You can't call non-const methods of a const object (or through a const reference or pointer).
If your node::get() function doesn't modify the object, it should be declared as a const method:
template <class T>
class node
{
int get() const { /*...*/ }
};

Share this post


Link to post
Share on other sites
me22    212
Note that a const member function can return a const member pointer to a non-const something -- const isn't "deep".

struct Node {
Node *other;
Node *const get() const { return other; } // not a problem
};


But yes, using const wherever logical is a very good plan, as is not using const_cast. ( Remember that you can use mutable members if you need to change things in member functions that intuitively should be const -- such as cached lengths or similar. )

Share this post


Link to post
Share on other sites
iMalc    2466
Quote:
Original post by Dave
Not enough programmers are const correct, in my opinion. If you become const correct then fair play to you!
Seconded!
Did you rename yourself ace?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
i agree that const correctness is a nice thing to have, saving us of quite a bit of trouble.

to just cite another view on the topic: " const is usefull as long as it can be casted away " (anders heijlsberg).. i think he didn't mean it that serious, though :)

Share this post


Link to post
Share on other sites
meh    375
Quote:
Original post by Dave
Not enough programmers are const correct, in my opinion. If you become const correct then fair play to you!


Definately, it's also an area of C++ that most people really overlook as you don't *need* to do it. It can however stop you making some really insidious mistakes. Use it!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this