jacquesdr
asked on
Stupid Question probally, Array Pointer ?
HI all,
I have 2 classes lets say Class a(base class) & class b(derived class)
now the inhertance works more or less the problem is with a function in my base class
the declaration looks like this
......
protected:
void *** getArrayPtr() { return &array; }
/* primitive operations */
virtual unsigned int getNumberOfItems () const = NULL;
virtual void resize (unsigned int newSize) = NULL;
private:
void ** array;
Now how do i in my derived class to operations on the array ??
i tried
int *p = getArrayPtr();
but this gives erros ??
ps. yet again i'cannot change anythign in the base class
I have 2 classes lets say Class a(base class) & class b(derived class)
now the inhertance works more or less the problem is with a function in my base class
the declaration looks like this
......
protected:
void *** getArrayPtr() { return &array; }
/* primitive operations */
virtual unsigned int getNumberOfItems () const = NULL;
virtual void resize (unsigned int newSize) = NULL;
private:
void ** array;
Now how do i in my derived class to operations on the array ??
i tried
int *p = getArrayPtr();
but this gives erros ??
ps. yet again i'cannot change anythign in the base class
ASKER
Hye, Jkr
jkr it's workx but ;)
sorry i messed up ;)
the function in wich i calls it is a const function
unsigned int IntPtrStack::getNumberOfIt ems() const {
void ***p = getArrayPtr();
//return sizeof(p);
}
sorry i forgot to mention it ??
and if i use it like this it' still gives errors ;)
PS> you'll get the points;)
jkr it's workx but ;)
sorry i messed up ;)
the function in wich i calls it is a const function
unsigned int IntPtrStack::getNumberOfIt
void ***p = getArrayPtr();
//return sizeof(p);
}
sorry i forgot to mention it ??
and if i use it like this it' still gives errors ;)
PS> you'll get the points;)
>>the function in wich i calls it is a const function
So you have to make sure that the call is made in a 'const' context also, e.g. by adding a const member to your derived class:
void *** getArrayPtr() const { return &array; }
So you have to make sure that the call is made in a 'const' context also, e.g. by adding a const member to your derived class:
void *** getArrayPtr() const { return &array; }
ASKER
but whould this return the array of the derived class instead of the array in the baseclass ??
ASKER
ps . i need to use the array in the base class, ps i 've heard sumwhere you can cast a thing to not have const"ness anymore ?
AS the array is inherited by the derived class, it makes no difference. Remember, inheriting from a base class means extending it's functionality by keeping the base (except the 'private' members).
>>i 've heard sumwhere you can cast a thing to not have const"ness anymore ?
You can, but it's difficult to do it the other way round :o)
>>i need to use the array in the base class
Unless you add an 'array' member to your derived class, that's what is going to happen.
You can, but it's difficult to do it the other way round :o)
>>i need to use the array in the base class
Unless you add an 'array' member to your derived class, that's what is going to happen.
ASKER
if i use "void *** getArrayPtr() const { return &array;}" in my derived class i get...
IntPtrStack.h:17: error: within this context
IntPtrStack.h:17: error: invalid conversion from `void** const*' to `void***'
IntPtrStack.h:17: error: within this context
IntPtrStack.h:17: error: invalid conversion from `void** const*' to `void***'
Hmm, GCC with it "precise" error messages :o)
Try
const void *** getArrayPtr() const { return &array;}"
Try
const void *** getArrayPtr() const { return &array;}"
ASKER
LOL ;))) i'll give it a bash ;)
ASKER
nada ;((
but array in the base class is a private member ? and i can't change that ;(
and other ways you can think of ?
i tried to look at that const_cast thin but's it's hope less concernign my c++ knowledge ;(
but array in the base class is a private member ? and i can't change that ;(
and other ways you can think of ?
i tried to look at that const_cast thin but's it's hope less concernign my c++ knowledge ;(
Shoot, you're right - I thought it was 'protected'.
So, what is the exact error message you get with
unsigned int IntPtrStack::getNumberOfIt ems() const {
void ***p = getArrayPtr();
}
or
unsigned int IntPtrStack::getNumberOfIt ems() const {
const void ***p = getArrayPtr();
}
?
So, what is the exact error message you get with
unsigned int IntPtrStack::getNumberOfIt
void ***p = getArrayPtr();
}
or
unsigned int IntPtrStack::getNumberOfIt
const void ***p = getArrayPtr();
}
?
ASKER
with void ***p = getArrayPtr();
IntPtrStack.C: In member function `virtual unsigned int
IntPtrStack::getNumberOfIt ems() const':
IntPtrStack.C:15: error: passing `const IntPtrStack' as `this' argument of `
void*** myutilities::TemplateStack ::getArray Ptr()' discards qualifiers
-----------
and with const void ***p = getArrayPtr();
IntPtrStack.C: In member function `virtual unsigned int
IntPtrStack::getNumberOfIt ems() const':
IntPtrStack.C:15: error: passing `const IntPtrStack' as `this' argument of `
void*** myutilities::TemplateStack ::getArray Ptr()' discards qualifiers
IntPtrStack.C:15: error: invalid conversion from `void***' to `const void***'
;)
IntPtrStack.C: In member function `virtual unsigned int
IntPtrStack::getNumberOfIt
IntPtrStack.C:15: error: passing `const IntPtrStack' as `this' argument of `
void*** myutilities::TemplateStack
-----------
and with const void ***p = getArrayPtr();
IntPtrStack.C: In member function `virtual unsigned int
IntPtrStack::getNumberOfIt
IntPtrStack.C:15: error: passing `const IntPtrStack' as `this' argument of `
void*** myutilities::TemplateStack
IntPtrStack.C:15: error: invalid conversion from `void***' to `const void***'
;)
Hmm, that's bad, what I expected. After re-reading your other Q at https://www.experts-exchange.com/questions/20935110/Inheritance-Why-is-my-code-not-working.html , I think you have chosen the wrong approach, as you are not expected to call non-const members from const members - what about using 'unsigned int size () const;'?
unsigned int IntPtrStack::getNumberOfIt ems() const {
return size ();
}
unsigned int IntPtrStack::getNumberOfIt
return size ();
}
ASKER
aaah ok ,
but then size returns
getNumberOfItems ???
but then size returns
getNumberOfItems ???
ASKER
size as seen inTemplate class is basicly just a callign function of getNumberofItems ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>int *p = getArrayPtr();
>>but this gives erros ??
According to the declaration of the method, it should be
int ***p = getArrayPtr();
However, you might be better off using
protected:
void **& getArrayPtr() { return array; }
to return a reference to the array and use
int **p = getArrayPtr();
which makes the code less clumsy to use in terms of pointer indirections.