• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

inheritance

Hello

I have a two classes :

class X is a base class
class XA is publicly derived from class X

when I create array of pointers of class X

X** XArray;

then I change the type to XA object

XArray[0] = new XA;

and try access read() function defined in XA class

XArray[0]->read();

I get a error message that :
In function 'int main()':
error: 'class X' has no member named 'read'

Why there is still function call to X::read() ?

Anyone have any idea what I'm doing wrong or what am I missing here?

compiler : gcc version 4.0.3 (Debian 4.0.3-1)

thank you
0
Luxana
Asked:
Luxana
  • 6
  • 3
1 Solution
 
rajeev_devinCommented:
>> X** XArray;
First of all I don't think you have allocated the array of pointers.

Something like this
X** XArray;
XArray = new X*[10];

Else you will get segmentation fault in
XArray[0] = new XA;
0
 
rajeev_devinCommented:
Now coming to your question do you have any read() function in class X ?
If it is not there then you will get compilation error.
0
 
rajeev_devinCommented:
If you have read function in XA class and you are trying to call it using
the base class pointer then its not a valid.

class X
{
   virtual void read(); // You should have it here virtual
};

class XA : class X
{
   void read(){...} // If the read of base is virtual then this function will be called because of runtime polymorphism
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rajeev_devinCommented:
If you don't want to write this function in class X.
Then make a call like this

Typecast pointer of X to XA

((XA*)XArray[0])->read();

good luck :)
0
 
LuxanaAuthor Commented:
Hi rajeev;
sorry I forgot one line:-))
      
XArray = new X*[10]; is there


so where is the problem actually?

0
 
LuxanaAuthor Commented:
ok I will try that trick with virtual function in X class. rajeev is there any way to verify that XArray is of type XA?



0
 
rajeev_devinCommented:
>> rajeev is there any way to verify that XArray is of type XA?
You need to use RTTI (Runtime type information) for that.

Or, else put some variable in your base class and use that variable to find the type.
Example:

class X
{
public:
   X(int t = 0) : type(t) {} // 0 for X
   int getType() const {return type;}

protected:
   int type;
}

class XA : public X
{
public:
   XA():X(1) // 1 for XA
}

class XB : public X
{
public:
   XB():X(2) // 2 for XB
}

X* x = new X;
if (x->getType() == 0)
{
   // It is of type X.
}
else if (x->getType() == 1)
{
   // It is of type XA.
}
else if (x->getType() == 2)
{
   // It is of type XB.
}
else
{
   // Unknown type.
}
0
 
rajeev_devinCommented:
0
 
LuxanaAuthor Commented:
excellent !! I got it work finally:-)) A bit pain for a first time :-))


thanks for very informative answer

lubo
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now