Protected data can't be accesed

A

atomik.fungus

Hi, im having problems compiling some code, and i dont understand why.
I've made a Matrix class for any kind of data and now im implementing
an inherited class with all the mathematical stuff (MMatrix)
(determinants and so on).
The data and the number of rows and columns are declared in the base
clase as protected. When i ask the functions of the inherited class to
use them, the compiler(g++) tells me that they haven't been declared in
that ambit( sorry for the poor translation but errors are in spanish).
Here is the code for the inherited class. (The base class works
perfectly )
The code for the constructors has been copy pasted from the base class
in a desperate try. Originally it wasnt there only the initialization
to 0 of the matrix but the problem eas the same.

#ifndef MMATRIX_CPP
#define MMATRIX_CPP

#include <iostream>
#include <cstdlib>
#include "matrix.cpp"

using namespace std;

//Definicion de la clases

template< class T >
class MMatrix : public Matrix< T > //Hereda de las matrices
{
public:
MMatrix( unsigned = 3, unsigned = 3 );
MMatrix( const MMatrix< T > & ); //Constructor de copia
};

#endif

/*mmatrix.hpp*/
#include "mmatrix.cpp"
#include <iostream>
#include <cstdlib>

using namespace std;

//Constructor para las nuevas matrices identico al otro
template< class T >
MMatrix< T >::MMatrix( unsigned rows, unsigned columns )
{
int i, j;

assert( rows > 0 );
assert( columns > 0 );

r_size = rows; //Si no se pasa el tamaño se asume 3
c_size = columns; //Si no se pasa el tamaño se asume 3

data = new T*[ rows ];
for( i = 0; i < rows; i++ )
data[ i ] = new T[ columns ];

assert( data != 0 );

for( i = 1; i <= r_size; i++ ) {
for( j = 1; j <= c_size; j++ ) This( i, j ) = 0;
}
}

template< class T >
MMatrix< T >::MMatrix( const MMatrix< T > &init ) : r_size( init.r_size
), c_size( init.c_size )
{
int i, j;

data = new T*[ r_size ];
for( i = 0; i < r_size; i++ )
data[ i ] = new T[ c_size ];

assert( data != 0 );

for( i = 0; i < r_size; i++ ) {
for( j = 0; j < c_size; j++ )
data[ i ][ j ] = init.data[ i ][ j ];
}
}

I also have some problem working with several files and the
preprocessor, but thats another problem.

Thanks.

P.D. "This" is a macro "#define This( i, j ) this->operator()( (i), (j)
)" in the base class archive
 
V

Victor Bazarov

Hi, im having problems compiling some code, and i dont understand why.
I've made a Matrix class for any kind of data and now im implementing
an inherited class with all the mathematical stuff (MMatrix)
(determinants and so on).
The data and the number of rows and columns are declared in the base
clase as protected. When i ask the functions of the inherited class to
use them, the compiler(g++) tells me that they haven't been declared
in that ambit( sorry for the poor translation but errors are in
spanish). Here is the code for the inherited class. (The base class
works perfectly )
[..]

I think you're running into the same problem many have run into before
you:

class base {
protected:
int i;
};
class derived {
public:
void foo(base const& b) {
b.i; // prohibited -- this object doesn't have access
}
};

Yes, it's truly prohibited. The object can only access _its_own_
protected members, not other object's.

V
 
M

Mike Stevenson

I think you're running into the same problem many have run into before
you:

class base {
protected:
int i;
};
class derived {
public:
void foo(base const& b) {
b.i; // prohibited -- this object doesn't have access
}
};

Yes, it's truly prohibited. The object can only access _its_own_
protected members, not other object's.

V

Isn't the whole point of the protected keyword to provide direct access
to some member data and functions only to the base class and its derived
classes? In your example derived does not inherit from base. This code
should demonstrate that.

#include <iostream>
class foo{
public:
int GetA() { return itsA; }
void SetA(int a) { itsA = a; }
protected:
int itsA;
};

class bar : public foo{
public:
int AnotherGetA() { return itsA; }
};

int main(){
bar myBar;
myBar.SetA(10);

std::cout << myBar.GetA() << std::endl;
std::cout << myBar.AnotherGetA() << std::endl;
return 0;
}

Also, can't an object directly access the private members of ANY object
of its class, not just itself?
 
A

atomik.fungus

I've doen my program to see if that was the problem and it works
perfectly.

#include <iostream>

using namespace std;

class base {
protected:
base();
int i;
};
class derived:public base {
public:
void foo() { cout << i; }
};
base::base() { i=99; }

int main()
{
derived test;
test.foo();
return 0;
}
 
M

Marcus Kwok

Hi, im having problems compiling some code, and i dont understand why.
I've made a Matrix class for any kind of data and now im implementing
an inherited class with all the mathematical stuff (MMatrix)
(determinants and so on).
The data and the number of rows and columns are declared in the base
clase as protected. When i ask the functions of the inherited class to
use them, the compiler(g++) tells me that they haven't been declared in
that ambit( sorry for the poor translation but errors are in spanish).

Maybe this will help:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18
 
H

Howard

Mike Stevenson said:
Isn't the whole point of the protected keyword to provide direct access
to some member data and functions only to the base class and its derived
classes? In your example derived does not inherit from base.

I'm pretty sure that was an oversight on Victor's part. With the ": public
base" added to the derived class, it would demonstrate the problem
correctly.
This code
should demonstrate that.

#include <iostream>
class foo{
public:
int GetA() { return itsA; }
void SetA(int a) { itsA = a; }
protected:
int itsA;
};

class bar : public foo{
public:
int AnotherGetA() { return itsA; }
};

int main(){
bar myBar;
myBar.SetA(10);

std::cout << myBar.GetA() << std::endl;
std::cout << myBar.AnotherGetA() << std::endl;
return 0;
}

That doesn't match the original post, because it never tries to access the
protected member of a different object. Both functions simply access
this->istA. The original problem was how to get access to the protected
member of an object passed as a parameter, not of the current object.
Also, can't an object directly access the private members of ANY object
of its class, not just itself?

Nope. Only through the "this" pointer. (Think of it like this: a
SportsCar, derived from a Car, should have access to its engine (which is
part of the base class Car), but it should _not_ have access to the engine
of _another_ Car!)

-Howard
 
H

Howard

Howard said:
Nope. Only through the "this" pointer. (Think of it like this: a
SportsCar, derived from a Car, should have access to its engine (which is
part of the base class Car), but it should _not_ have access to the engine
of _another_ Car!)

Oops, sorry! The restriction regarding access via the "this" pointer only
applies to derived classes and access to the protected members of the base,
not to private/protected members of the _same_ class. (And the anaology is
therefore pretty lame as well.)

-Howard
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top