// @topic T00760 Initializer lists and const qualifiers
// @brief 10/18/2012 -- const data members, const references, const member functions
#include <iostream>
#include <string>
class Graph {};
class Point {
const std::string m_tag;
Graph& m_g;
int m_x;
int m_y;
public:
Point( Graph& gr, int xx, int yy )
:
m_tag( get_my_tag() ), // ctr initializer list
m_g( gr ),
m_x( xx ),
m_y( yy )
{
}
// print() doesn't change anything, so it is made const
void print() const
{
std::cout << get_my_tag() << " " << m_x << ":" << m_y;
}
// this is also const member function
std::string get_my_tag() const
{
return "demo";
}
};//class Point
// function foo() takes a const reference to an object:
void foo( Point const& pt )
{
// because Point::print() is a const member function,
// it can be invoked using const reference pt:
pt.print();
}
int main()
{
Graph gr;
Point pt_origin( gr, 10, 20 );
foo( pt_origin );
return 0;
}