![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jan 2006
Posts: 58
Rep Power: 3
![]() |
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; |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|