Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 28th, 2006, 7:31 PM   #1
sackarias
Programmer
 
sackarias's Avatar
 
Join Date: Jan 2006
Posts: 58
Rep Power: 3 sackarias is on a distinguished road
pointer to array of a class

I want to make a pointer in one class that points to an array of another class.

Will this code

//in classA
private :
   classB *thePointerToClassB;

make thePointerToClassB point at an array of classB without including [].

If it does, can I simply do something like

*thePointerToClass[idx] = something;
sackarias is offline   Reply With Quote
Old Mar 28th, 2006, 7:37 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
I am a bit rusty in C / C++ ATM (been having fun with Ruby), but I am 95% sure that your code will produce a pointer to a single object.

To make an array of pointers, you could simply do this:
class A
{
    ...
    private:
        B *classAPointer[];
};

Though the joys of std::vector are endless when compared to C arrays.

(please correct any misinformation (you know who you are ... ) )

EDIT:

I'm actually unsure in this case, because the following code works exactly as expected:

#include <iostream>

int main() {
    int *numbs = new int[10];

    for(int x = 0; x < 10; x++) {
        numbs[x] = x + 1;
        std::cout << numbs[x] << std::endl;
    }

    delete [] numbs;

    return 0;
}
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Mar 28th, 2006, 8:04 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
A raw pointer points to an object in memory. It has no size/dimensional information beyond what you can sneak into the code as a hint to the compiler.

int myArray [10][20]; is an array, lets say. If you define a function thusly:

void myFunc (int a [][20]) and call it thusly:

myFunc (myArray);, a pointer to myArray gets passed. If you define it, rather, as:

void myFunc (int *a) and call it,

myFunc (myArray);, the exact same pointer gets passed.

It contains no size/dimension information; you need to pass that as an additional argument. HOWEVER, the first definition is a hint to the compiler that the lowest dimension is 20. Therefore, inside the function, ptr++ will increment by 20 integers. In the second example, it will increment by one integer. The first definition implies that it is a pointer to an ARRAY OF INTS, whereas the second implies that it is a pointer to int.

It's important to separate in your mind what information contained in your code causes explicit actions in the emitted machine code, and what information is a hint or implicit suggestion regarding the code to be emitted.

One cannot assign an array of char directly. One must do it piecemeal. Nevertheless, the syntax,

char myArray [10] = "Abcd"; certainly appears to be doing that.

It isn't, in the real sense that the emitted code is doing it. The compiler is merely being nice to you in a way that may confuse you when you get beyond an operation that can be done for you at compile time, contrasted to what may be done for you at run time.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC