Solved

Stupid Question probally, Array Pointer ?

Posted on 2004-03-28
17
326 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

632 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