Hello everybody!
I'm trying to understand following code.

#include "stdafx.h"
#include <iostream>
using namespace std;
class Circle;
class Square;
class Triangle;

class Shape {
public:
	virtual bool Check(const Shape&)const = 0;
	virtual bool Check(const Circle&)const = 0;
	virtual bool Check(const Square&)const = 0;
	virtual bool Check(const Triangle&)const = 0;
};
class Circle : public Shape {

public:
	bool Check(const Shape& s)const
		{return s.Check(*this);}
	bool Check(const Circle&)const
		{cout << "Circle & Circle\n"; return true;}
	bool Check(const Square&)const
		{cout << "Circle & Square\n"; return true;}
	bool Check(const Triangle&)const
		{cout << "Circle & Triangle\n"; return true;}
};
class Square : public Shape {

public:
	bool Check(const Shape& s) const
		{return s.Check(*this);}
    bool Check(const Circle& c)const
		{return c.Check(*this);}
	bool Check(const Square&)const
		{cout << "Square & Square\n"; return true;}
    bool Check(const Triangle&)const
		{cout << "Square & Triangle\n"; return true;}
};
class Triangle : public Shape {

public:
	bool Check(const Shape& s)const{return s.Check(*this);}
	bool Check(const Circle& c)const{return c.Check(*this);}
	bool Check(const Square& s)const{return s.Check(*this);}
	bool Check(const Triangle&)const
		{cout << "Triangle & Triangle\n"; return true;}
};
bool intersect(const Shape& s,const Shape& t)
	{return s.Check(t);}

int _tmain(int argc, _TCHAR* argv[])
{
	Circle      c;
	Square      s;
	Triangle    t;
	intersect(c,s);
	intersect(t,c);
	intersect(s,t);
	intersect(t,t);
	system("PAUSE");
	return 0;
}

What I don't understand is: the second "const", that is using before every Check's body function like: bool Check(const Shape& s)const{return s.Check(*this);}.
I can not find something similar to this in my ebooks.
So I need your helps.
Thanks in advanced!

the const keyword following a function declaration means the function will not change anything.

The trailing const on a non-static member function means that the abstract (logical, externally visible) state of the object pointed to by this will not be changed by this member function. Where possible, the compiler enforces this rule. However, the const specifier does not promise that the physical state ("raw bits") of the object will never change.

struct mystring
{
    private:

      int len ; // number of chars in the mystring; part of it's logical state

      char* p ; // points to the (first char of) the actual chars of the mystring
                // every char in this buffer forms part of the logical state
                // if we allocate a new buffer, copy the contents of the current buffer
                // into the new buffer, and then modify p to point to the new buffer,
                // the logical state of mystring does not change
    public:

      // ... mystring member functions

      // *** compile-time error - attempt to change the const object *this
      void foo() const { ++len ; }

      // fine by the compiler - no attempt to change the member p
      // see: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.15
      // *** but it is a logical error - changes the logical state of a const object
      void bar() const { if(len>0) p[0] = 'a' ; }

      // fine  - it is ok to change a different (non-const) object
      void baz() const { mystring another ; ++another.len ; /* ... */ }

      // fine  - mutable members are never const;
      // they do not contribute to the logical state of the object
      void foobar() const { ++num_foobar_calls ; /* ... */ }

      private: mutable int num_foobar_calls ; // not part of logical state
};

See: http://www.parashift.com/c++-faq-lite/const-correctness.html
http://en.wikipedia.org/wiki/Const-correctness#Methods

The trailing const on a non-static member function means that the abstract (logical, externally visible) state of the object pointed to by this will not be changed by this member function. Where possible, the compiler enforces this rule. However, the const specifier does not promise that the physical state ("raw bits") of the object will never change.

Whew! All that gobbledegook to just say what I did in my previous post. Sounds like it came straight out of a text book :)

thank you very much. One more problem. Now I've removed all const key word of that type. After that the programm did not work. Why that happened?
Need more help, please!

what do you mean by "did not work" ??? We can't see your monitor.

Means: there're some errors while compiling.
here: bool Check(const Shape& s)

{return s.Check(*this);}
Error1: error C2663: 'Shape::Check' : 4 overloads have no legal conversion for 'this' pointer

You need to change the class declaration too (probably in a header file).

I've created Shape.h (without const keyword) and still error here:

bool intersect(const Shape& s,const Shape& t)
{
    return  s.Check(t);
}
Error   1   error C2663: 'Shape::Check' : 4 overloads have no legal conversion for 'this' pointer

look at the parameters for Shape::Check -- do they contain const keyword? t is const in intersect() so it can't be passed to a function whose parameter is not const. And yes, getting all those const straight is a pain in the behind :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.