ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I 've been searching for an answer to this like a madman and found nothing, so please help. If I have an array of pointers to class objects, I can only access member functions using the dot operator as opposed to the arrow operator. i.e
Obj *pntr = new Obj[5];
obj[0].func(); //I have to use this syntax as opposed to
obj[0]->func();
Is my compiler on crack? when I use the g++ compilers at my school, they tell me to change my major if I use the dot operator like this. But the compiler at home chokes on the arrow operator and says:"base is of non Obj pointer type" as if the '[]' operator is dereferencing the pointer. The weird thing is that if I have just a single pointer to a class object, I have to use the arrow operator to access member functions. Has there been some change in C++ pointer syntax that I'm unaware of?
Well I've tried it, and it works for me on gcc 2.95.4. This program works as expected:
Code:
#include <cstdio>
using namespace std;
class Foo
{
public:
int bar;
Foo()
{
bar = 5;
}
};
int main(int argc, char **argv)
{
Foo *foos = new Foo[5];
printf("%i\n", foos[2].bar);
return 0;
}
Btw, the [] operator DOES dereference the pointer - someArray[x] is just shorthand for:
/* Nasty pointer arithmetic */
*(someArray + x)
Therefore [] will always give you a value one level of reference lower than the value it was applied to; i.e. a pointer a goes to a value, a pointer to a pointer goes to a pointer, etc...
Alex
Last edited by llama_meme; 11-20-2002 at 06:58 AM.
According to this declaration pntr is a pointer to an array of five Obj objects, not an array of pointers to Obj objects how you wanted.
If you wanted an array of pointers to Obj objects you must declare pntr like this :
Obj **pntr = new *Obj[5];
Now pntr points to an array of five pointers to Obj objects. You must allocate each pointer in the array to point to a valid memory space:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.