Inheritance Question...

This will be an easy one.

How can I access a base class public method from a derived class method. I don't know why I'm having trouble with this. Here is some code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace lap{


class Matrix{
 public:
  double& operator()(unsigned int row, unsigned int col);
}

class RowVector : public Matrix{
 public:
  double& operator()(unsigned int col) { return (*this)(0,col); }
}


}


I get the following error:

no match for call to '(const lap::RowVector) (int, const unsigned int&)
candidates are: double& lap::RowVector::operator()(unsigned int) const

When I replace it with it works fine:

1
2
3
4
5
6

class RowVector : public Matrix{
 public:
  double& operator()(unsigned int col) { return lap::Matrix::operator()(0,col); 
}


Not sure what is going on here. Any help would be great.

Thanks! Nick.
Last edited on
I'm not completely sure why your solution is working really but...

this is a pointer to RowVector since you are in the RowVector class, and there using the operator() will only look at the RowVector's methods. You will have to use this->Matrix::operator()(0, col)

EDIT: You could also use a cast to cast it to your base class then call operator().
Last edited on
Hidden methods http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9
1
2
class RowVector : public Matrix{
  using Matrix::operator();
Last edited on
Thanks to both of you. All worked out!
Topic archived. No new replies allowed.