Can't Add Const to Friend Function

I

Immortal Nephi

I tried to add const to the global function. It becomes friend with
class A. Friend allows global function to modify private data
member. Need to add const and prevent global function from modifying
data member.
Why did C++ Compiler give error like C2662? No const added is
allowed. Explain? What is another solution?

class A
{
public:
A() {}
~A() {}

friend void Test( const A &r );

void Run()
{
Test( *this );
}

private:
int m_a;

int get_a()
{
return m_a;
}
};

void Test( const A &r ) // add const?
{
r.get_a(); // OK
r.m_a = 2; // should compile with error
}

int main()
{
A a;
a.Run();

return 0;
}
 
N

Neelesh

        I tried to add const to the global function.  
That is not allowed. "const" can be added only to member functions.
Why? Because if const is not added to a member function then the
member function can easily modify any non-const data member of the
class.
It becomes friend with class A.  Friend allows global function to modify private data
member.  
Need to add const and prevent global function from modifying data member.

If you want to avoid a global function from modifying the data members
of a class, make the function take a reference-to-const. Note that a
friend function is allowed to modify the data member of an object if
the object itself is not const. See this example:

class A
{
int t; //private data member
friend void foo(const A&);
};

void foo(const A& x)
{

// x.t = 10; //Error, modifying member of a const object is not
allowed
std::cout << x.t << std::endl; //Ok, accessing private member
allowed since foo is a friend of X
}


        Why did C++ Compiler give error like C2662?

C2662 is a compiler-specific error, AFAIK specific to Microsoft
Compiler, and it indicates inability to convert "this" pointer from
one type to another. See below regarding what this error means.
class A
{
public:
        A() {}
        ~A() {}

        friend void Test( const A &r );

        void Run()
        {
                Test( *this );
        }

private:
        int m_a;

        int get_a()
        {
                return m_a;
        }

};

void Test( const A &r ) // add const?
{
        r.get_a(); // OK

Note that since r is a reference-to-const, only const member functions
can be invoked on r. This means that the above line will give error as
get_a is not a const member function

In other words, the member function get_a() requires its first
(implicit) argument as a pointer-to-non-const-object whereas r is a
const in this context. Thus, the compiler cannot convert this-pointer-
to-const to this-pointer-to-non-const and hence it throws C2662
error.
        r.m_a = 2; // should compile with error

What is exactly "compiling with error"? If you mean that this line
should give error, then it will anyway give an error since r is a
reference-to-const.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top