Archived

This topic is now archived and is closed to further replies.

Leonscape

Operator Overloading

Recommended Posts

Okay I've searched and searched, but I seem to be missing something obvious. When I inherit a base class, my derived class should have access to the protected members, but when I try to access the protected members of another object, ( i.e. when overloading an operator ) I'm getting compile errors saying this is not allowed in that context. So how do I do it. This happens for functions as well as data members. an example:
class A
{
protected:
   int m_value;
public:

   A( int value )
   {
      m_value = value;
   }

   A& operator=( const A &obj )
   {
      m_value = obj.m_value;
      return *this;
   }

   virtual int getValue()
   {
      return m_value;
   }
};

class B : public A
{
   int m_value2;
public:

   B( int value, int value2 ) : A( value )
   {
      m_value2 = value2;
   }

   B& operator=( const B &obj )
   {
      m_value = obj.m_value;  //<----- This is not allowed

      m_value2 = obj.m_value2;
      return *this;
   }

   int getValue()
   {
      return m_value2 + m_value;
   }
};
[edited by - Leonscape on March 19, 2004 11:50:07 AM]

Share this post


Link to post
Share on other sites
quote:
m_value = obj.m_value; //<----- This is not allowed


Who says it''s not allowed? It should be okay.

Are you getting an error message from your compiler? What is it and what compiler are you using?

Share this post


Link to post
Share on other sites
Slight mistype there ( sorry ). That function should be...


B& operator=( const A &obj )
{
m_value = obj.m_value;
return *this;
}


I need this, despite is being the same as the function in the a class becuase I''m getting errors for


A obja;
B objb;

objb = obja;


None of it will compile always "error in this context".

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There''s nothing wrong in your source code.
Try another compiler such as GCC

Share this post


Link to post
Share on other sites
This is GCC

I've broke it down to a program if you want to test it


#include <iostream>

using namespace std;

class A
{
protected:
int m_value;
};

class B : public A
{
int m_value2;
public:
B& operator=( const A &obj )
{
m_value = obj.m_value; //< Context error

return *this;
}

B& operator=( const B &obj )
{
m_value = obj.m_value; //< No error

m_value2 = obj.m_value2;
return *this;
}
};

int main(int argc, char *argv[])
{
A obja;
B objb;

objb = obja;
return 0;
}


[edited by - Leonscape on March 19, 2004 12:34:39 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Leonscape
Slight mistype there ( sorry ). That function should be...


B& operator=( const A &obj )
{
m_value = obj.m_value;
return *this;
}




which class is that on? A or B?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yep, you can''t do that.
m_value is a Protected A member.

Share this post


Link to post
Share on other sites
Yep m_value is a protected member of A,but B inherits A, so B should have access to the protected members of A.

But it can''t have access to m_value when its passed an A object.

So how do I access the protected member without breaking the encapsulation and making the protected member accessable through public functions?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You define an operator= for class A, then in your B::operator=()
you call A::operator=(objA);

Hope that made sense.

Share this post


Link to post
Share on other sites
In the = operator obj is an instance of A, so you can''t access to its m_value member.

You have to add an GetValue method to A, and use that.

Share this post


Link to post
Share on other sites