Solved

Stupid Question probally, Array Pointer ?

Posted on 2004-03-28
17
323 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

820 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