Solved

Stupid Question probally, Array Pointer ?

Posted on 2004-03-28
17
320 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 126
How to split this in C++ 4 76
Dialogbox API leak? 18 59
Add values of each row in an array 3 29
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…
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…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

13 Experts available now in Live!

Get 1:1 Help Now