usage of [] operator with pointers

P

Pavan

Hi,
My application requires usage of overloaded operator [] using
pointer
My application is like this

vector<int> * ptr;

Now I should access elements of the vector using vector pointer but
the below code is giving compilation error:

for(unsigned int i=0; i < ptr->size(); i++)
{
cout << ptr-> << endl; // I know that while calling as a member
we dont use dot operator before [] like how I am using -> here, so is
it not possible to access elements using [] with pointer at all? Is
there any alternative
}
 
G

gob00st

Hi,
    My application requires usage of overloaded operator [] using
pointer
My application is like this

vector<int> * ptr;

Now I should access elements of the vector using vector pointer but
the below code is giving compilation error:

for(unsigned int i=0; i < ptr->size(); i++)
{
   cout << ptr-> << endl; // I know that while calling as a member
we dont use dot operator before [] like how I am using -> here, so is
it not possible to access elements using [] with pointer at all? Is
there any alternative

}


Try
cout << (*ptr) << endl;

Gob00st
 
P

peter koch

Hi,
    My application requires usage of overloaded operator [] using
pointer
My application is like this

vector<int> * ptr;
Why not vector<int> vec;

Your construct looks as if you've had the Java-disease. Don't allocate
dynamically in C++ unless you have good reason.


/Peter
(as told elsethread, the solution to your problem would be to use *ptr
instead of ptr->)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top