--- In [email protected], Gopi Krishna Komanduri <gopikomand...@...> wrote:
>
> ... I know that we can over loas const member functions
> by removing const. consider that I overloaded a const
> member function. Now consider that I declared a normal
> object (non-const). Now how can I access the const member
> functyion from non const object. As by default , this non
> const object will access non-const member function.
> example:
> class Base
> {
> public:
>
> Base()
> {
> //printf("\n I am in Base cons");
> }
> void show(int i) const
> {
> printf("\n the val if i is %d and is cons method",i);
Don't use printf in C++ programs unless you have to.
Also, \n goes at the end of a line, not the start. Lines in
text streams (like stdout) should be terminated with a \n.
> }
> void show(int i)
> {
> printf("\n I am in non const method");
> }
> };
> void main()
main returns int, not void.
> {
> Base obj;
> Base const obj1;
> obj.show(20);
> obj1.show(20);
>
> // if I want to call show of non const using obj , if this not possible?
>
> }
#include <iostream>
#include <string>
struct object_t
{
object_t(const std::string &ss) : s(ss) { }
void show() const
{
std::cout << s << ": object_t::show() const" << std::endl;
}
void show()
{
std::cout << s << ": object_t::show()" << std::endl;
}
private:
std::string s;
};
int main()
{
object_t obj1("obj1");
const object_t obj2("obj2 (const)");
obj1.show();
const_cast<const object_t &>(obj1).show();
obj2.show();
}
--
Peter