Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using class pointers vs instance

What I don't understand is what is the difference between using a pointer to a class and generating a new instance of it. It's just for performance? Here I made a class and made m the pointer to the class and n the instance of the class. And another question: can i make a pointer the class and use another constructor? like myClass* p(7); p->afis(); ?

#include <iostream>
using namespace std;

class myClass
{
    int a;
public:
    myClass(void);
    myClass(int);
    void afis();
    ~myClass(void);
};

myClass::myClass(void)
{
    a = 5;
}

myClass::myClass(int nr)
{
    a = nr;
}

void myClass::afis()
{
    cout << a;
}

myClass::~myClass()
{
}

int main()
{
    myClass* m;                                     //<--
    m->afis();

    myClass n(7);                                   //<--
    n.afis();

    cin.get();
}
like image 693
Dementor Avatar asked Jan 19 '12 18:01

Dementor


People also ask

Is an instance of a class a pointer?

So, in Objective-C, if a variable is an instance of the class MyClass, that variable is of type MyClass* — a pointer to a MyClass. In general, in Objective-C, a reference to an instance is a pointer and the name of the data type of what's at the far end of that pointer is the name of the instance's class.

Why do we use pointer to class?

Pointers are used for file handling. Pointers are used to allocate memory dynamically. In C++, a pointer declared to a base class could access the object of a derived class. However, a pointer to a derived class cannot access the object of a base class.

Why do we use pointers in CPP?

Pointers are used extensively in both C and C++ for three main purposes: to allocate new objects on the heap, to pass functions to other functions. to iterate over elements in arrays or other data structures.

How do you access a class pointer?

A pointer to a C++ class is done exactly the same way as a pointer to a structure and to access members of a pointer to a class you use the member access operator -> operator, just as you do with pointers to structures. Also as with all pointers, you must initialize the pointer before using it.


1 Answers

myClass* m;   

is just an pointer to the type myClass it does not point to any valid object, dereferecing such a pointer is Undefined Behavior.

An Undefined Behavior means that your program is invalid and it may seem to work or it may crash or it may show any weird behavior, all safe bets are off. So just because your program works does not mean it is safe and it will always work.

To write a valid program you will have to make the pointer point to a valid object.
For example:

myClass obj;
myClass*m = &obj;

In the second case:

 myClass n(7);

It creates an object n of the type myClass by calling the constructor of myClass which takes one argument of the type int.
This is a valid way of creating an object.

like image 71
Alok Save Avatar answered Sep 20 '22 17:09

Alok Save