CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 24

Threaded View

  1. #8
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: howto restric a method to being absolutely constant

    Quote Originally Posted by ssouffri View Post
    I know const can be overridden in more than one way but using const is at least a very strong reminder that an object should only be read. The main problem I have with a plain const method is that it can still change the state of objects its class has a pointer to. I would like an "absolute_const" keyword to declare that a method can only access pointers as if they were declared as pointers to constant objects ( const TYPE *p; ). So basically an absolute_const method could only change the state of its arguments.
    I disagree with your assertion completely. A const member function cannot change the state of objects that it has a pointer to even if the pointer is non-const (at least not without some hacking).

    In this slightly modified example you can see that the const functor cannot call non-const functions on members of the class instance. It's not necessary to limit the functor to only use const* attributes. It makes no difference whether the pointer is const or not because the caller is const. Therefore, the caller cannot use a pointer to make function calls unless the function itself is declared as const. Therefore you are still adequately protected in my opinion.

    Code:
    class ValueCollector
    {
    public:
        void insert(int val)
        {
    	values.push_back(val);
        }
        
    private:
        std::vector<int> values;
    };
    
    struct SortVectorFunctor
    {
        SortVectorFunctor()
        {
    	theCollectorPtr = new ValueCollector;
        }
        ~SortVectorFunctor()
        {
    	delete theCollectorPtr;
        }
        
        bool operator()(const std::vector<int>& lhs, const std::vector<int>& rhs) const
        {
    	// compare the last elements of each vector.
    	bool ret(false);
    	if(lhs.size() == rhs.size() && lhs.size() > 0)
    	{
    	    std::vector<int>::size_type last = lhs.size() - 1;
    	    ret = lhs[last] < rhs[last];
    	    
    	    if(ret)
    	    {
    		theCollector.insert(rhs[last]);
    		theCollectorPtr->insert(rhs[last]);
    	    }
    	}
    	
    	return ret;
        }
        ValueCollector theCollector;
        ValueCollector* theCollectorPtr;
        
    };

    Quote Originally Posted by ssouffri View Post
    So basically an absolute_const method could only change the state of its arguments.
    Well if the absolute_const function can change anything than it isn't absolute_const is it? I don't really understand what you mean by its arguments. If the arguments are passed by value or by non-const ref or pointer, it can change them because they aren't const. On the other hand a const member function can not alter the state of the object it belongs to. Is that a problem for you? What did you mean by arguments? I think that you can do exactly what you want using the const qualifier. A const function can call non-const functions on input arguments if the input arguments aren't const. If a const function receives a non-const array as an input it can modify the input array but cannot modify the state of the object that it belongs to. Understand?
    Last edited by kempofighter; November 10th, 2009 at 01:47 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured