- arrow operator and dot operator class pointer

0 votes

In C++, we know that given a class pointer, we use the (->) arrow operator to access the members of that class, as seen here:

#include <iostream>
using namespace std;

class myclass{
    private:
        int a,b;
    public:
        void setdata(int i,int j){
            a=i;
            b=j;
        }
};

int main() {
    myclass *p;
    p = new myclass;
    p->setdata(5,6);
    return 0;
}

Then I create an array of myclass.

p=new myclass[10];

when I go to access myclass members through (->) arrow operator, I get the following error:

base operand of '->' has non-pointer type 'myclass'

However, when I access class members using the (.) operator, it works. 

These are the things that perplex me. Why do I have to use the (.) operator for a class array?

Aug 11 in C++ by Nicholas
• 6,240 points
43 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
1 answer

std::greater on a an std::pair of a double and a class

std::greater is simply a wrapper for a ...READ MORE

answered Jun 2 in C++ by Damon
• 4,960 points
93 views
0 votes
1 answer

abstract class and virtual functions

Is this class considered abstract because it only contains one virtual function?  I could still build an Animal object and use getFoodCost(); No. In C++, a "Abstract Class" is often a class having a single pure virtual function: class Abstract { public: virtual ...READ MORE

answered Jun 20 in C++ by Damon
• 4,960 points
27 views
0 votes
1 answer

How to use new[ ] and delete[ ] operator in C++

int main(){ char *str; ...READ MORE

answered Jun 20 in C++ by Damon
• 4,960 points
34 views
0 votes
0 answers

Difference between 'new operator' and 'operator new'?

What is difference between "new operator" and ...READ MORE

Jun 15 in C++ by Nicholas
• 6,240 points
26 views
0 votes
1 answer

What is a Class and Object in C++?

A Class is like a blueprint, an ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
33 views
0 votes
0 answers

What are the differences between a pointer variable and a reference variable?

What distinguishes a reference variable from a ...READ MORE

Jul 11 in C++ by Nicholas
• 6,240 points
33 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

answered Jun 2 in C++ by Damon
• 4,960 points
32 views
0 votes
1 answer

C++ "Object" class

No, there is no generic base class&nb ...READ MORE

answered Jun 2 in C++ by Damon
• 4,960 points
35 views
0 votes
1 answer

Explanation of function pointers

It's a little perplexing. Function type and pointer to function type are distinct kinds (no more similar than int and pointer to int).  However, in virtually all cases, a function type decays to a reference to a function type.  In this context, rotting roughly refers to conversion (there is a difference between type conversion and decaying, but you are probably not interested in it right now). What matters is that practically every time you use a function type, you end up with a reference to the function type.  But take note of the nearly - almost every time is not always! And there are rare circumstances where it does not. typedef void(functionPtr)(int); functionPtr fun = function; This code tries to clone one function to another (not the pointer! the function!)  However, this is not feasible since functions in C++ cannot be copied.  The compiler does not let this, and I'm surprised you got it compiled (you say you got linker errors?) Now for the code: typedef void(functionPtr)(int); functionPtr function; function(5); function does ...READ MORE

answered Jun 21 in C++ by Damon
• 4,960 points
29 views
0 votes
1 answer

In C++ abs( *a - *b) does not return absolute value of negative number

On the first line, you reallocated *a, and it is now utilising that new value on the second line.  int origa = *a; *a = abs(origa + ...READ MORE

answered Jun 27 in C++ by Damon
• 4,960 points
33 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP