Solved

Stupid Question probally, Array Pointer ?

Posted on 2004-03-28
17
321 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now