Solved

Stupid Question probally, Array Pointer ?

Posted on 2004-03-28
17
324 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:jacquesdr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10700005
>>i tried
>>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.


0
 

Author Comment

by:jacquesdr
ID: 10700055
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::getNumberOfItems() 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;)

0
 
LVL 86

Expert Comment

by:jkr
ID: 10700086
>>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; }
0
Technology Partners: 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!

 

Author Comment

by:jacquesdr
ID: 10700100
but whould this return the array of the derived class instead of the array in the baseclass ??

0
 

Author Comment

by:jacquesdr
ID: 10700105
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 ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 10700106
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).
0
 
LVL 86

Expert Comment

by:jkr
ID: 10700123
>>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.
0
 

Author Comment

by:jacquesdr
ID: 10700177
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***'
0
 
LVL 86

Expert Comment

by:jkr
ID: 10700221
Hmm, GCC with it "precise" error messages :o)

Try

const void *** getArrayPtr() const { return &array;}"
0
 

Author Comment

by:jacquesdr
ID: 10700233
LOL ;))) i'll give it a bash ;)
0
 

Author Comment

by:jacquesdr
ID: 10700246
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 ;(

0
 
LVL 86

Expert Comment

by:jkr
ID: 10700257
Shoot, you're right - I thought it was 'protected'.

So, what is the exact error message you get with

unsigned int IntPtrStack::getNumberOfItems() const {

void ***p = getArrayPtr();
}

or

unsigned int IntPtrStack::getNumberOfItems() const {

const void ***p = getArrayPtr();
}

?
0
 

Author Comment

by:jacquesdr
ID: 10700289
with void ***p = getArrayPtr();

IntPtrStack.C: In member function `virtual unsigned int
   IntPtrStack::getNumberOfItems() const':
IntPtrStack.C:15: error: passing `const IntPtrStack' as `this' argument of `
   void*** myutilities::TemplateStack::getArrayPtr()' discards qualifiers
-----------
and with const void ***p = getArrayPtr();

IntPtrStack.C: In member function `virtual unsigned int
   IntPtrStack::getNumberOfItems() const':
IntPtrStack.C:15: error: passing `const IntPtrStack' as `this' argument of `
   void*** myutilities::TemplateStack::getArrayPtr()' discards qualifiers
IntPtrStack.C:15: error: invalid conversion from `void***' to `const void***'

;)

0
 
LVL 86

Expert Comment

by:jkr
ID: 10700310
Hmm, that's bad, what I expected. After re-reading your other Q at http://www.experts-exchange.com:8080/Programming/Programming_Languages/Cplusplus/Q_20935110.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::getNumberOfItems() const {

   return size ();

}
0
 

Author Comment

by:jacquesdr
ID: 10700394
aaah ok ,
but then size returns
getNumberOfItems ???
0
 

Author Comment

by:jacquesdr
ID: 10700441
size as seen inTemplate class is basicly just a callign function of getNumberofItems ?
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 10705797
Well, without more information - it seems to me like that...
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question