LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-18-2002, 02:02 PM   #1
tdurden
LQ Newbie
 
Registered: Nov 2002
Location: California
Distribution: redhat
Posts: 3

Rep: Reputation: 0
arrays of pointers to class objects


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?
 
Old 11-18-2002, 02:04 PM   #2
tdurden
LQ Newbie
 
Registered: Nov 2002
Location: California
Distribution: redhat
Posts: 3

Original Poster
Rep: Reputation: 0
P.S I just started using linux, and my computer is running redhat 8.0 I think, whatever the latest version is.
 
Old 11-18-2002, 04:08 PM   #3
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
obj[x] gives you an object, not a pointer to an object, so you have to use a dot.

if you had:

int *foo = new int[7]

then foo[2] would be an int, not a pointer to an int.

Alex
 
Old 11-18-2002, 09:27 PM   #4
tdurden
LQ Newbie
 
Registered: Nov 2002
Location: California
Distribution: redhat
Posts: 3

Original Poster
Rep: Reputation: 0
Yes that works for integers, but it's not the same for class objects. Try it if you don't believe me.
 
Old 11-20-2002, 06:47 AM   #5
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
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.
 
Old 12-16-2002, 10:47 AM   #6
bgraur
LQ Newbie
 
Registered: Dec 2002
Location: Iasi
Distribution: Mandrake 8.2
Posts: 12

Rep: Reputation: 0
Quote:
Obj *pntr = new Obj[5];
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:

pntr[i] = new Obj(); for each i in [0,4];
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with c++ code/pointers/arrays drisay Programming 1 01-28-2005 08:24 AM
segmentation error c++ arrays with objects ajay_linux LQ Suggestions & Feedback 0 04-30-2004 11:21 AM
java arrays and objects titanium_geek Programming 13 03-07-2004 01:21 PM
help w/ pointers vs arrays PTBmilo Programming 3 04-10-2003 05:13 PM
C Question: arrays and pointers tundra Programming 7 07-19-2002 03:38 AM


All times are GMT -5. The time now is 08:04 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration