-1

I made a simple class to represent an x, y coordinate. It has an encapsulated int for both and the following constructor:

//point.h
Point(int x = 3, int y = 5); // not zero for example purposes

//point.cpp
Point::Point(int x, int y) : x(x), y(y) {}

I then have a file main.cpp:

#include "point.h"
#include <iostream>

int main() {
    Point p;
    std::cout << "x: " << p.getX() << " y: " << p.getY() << std::endl;
    p.setX(7);
    p.setY(9);
    std::cout << "x: " << p.getX() << " y: " << p.getY() << std::endl;
}

Coming from a Java background, I expect that this would come up with a null pointer, but it instead prints:

x: 3 y: 5
x: 7 y: 9

My question is why the heck does declaring a variable call the constructor?

Czipperz
  • 3,268
  • 2
  • 18
  • 25
  • 4
    What do you mean, "null pointer"? There are no variables of pointer type in your code, so nothing that could possibly have a value of `NULL`. `why the heck does declaring a variable call the constructor?` Because the C++ language specification requires that. – Igor Tandetnik Jun 28 '15 at 23:25
  • 3
    C++ is not Java. I suggest you get a good C++ book and don't make assumptions about C++ based on your Java background. – Blastfurnace Jun 28 '15 at 23:31
  • In C++ you don't really need to do the `new Point()` thing to instantiate the object. When you declare `Point P` it creates an object in the stack memory, since your constructor has default values, so the constructor gets called and your `x` and `y` get the default value – user007 Jun 28 '15 at 23:31
  • 2
    In Java you actually declare *references* to objects, that's why they can be null and why you do the `new Point` thing - you are binding the reference to an object created on the heap; in C++, the same syntax declares actual objects (in this case allocated on the stack and scope-bound to the function). – Matteo Italia Jun 28 '15 at 23:32
  • 2
    In C++, variables can be objects, and objects can appear as variables. Pointers and references are explicit language features that must be programmed explicitly. – Kerrek SB Jun 28 '15 at 23:39
  • 3
    This is a hopeless way to learn a new language. Get a decent introductory text. – David Heffernan Jun 28 '15 at 23:42

4 Answers4

5

If you are coming from java you are used to everything being allocated on the heap and using the new operator to create an instance of a class or datatype, it doesn't work like that in c++.

Point p;

is the same thing as

Point p = new Point();

in java, except for a few obvious things.

You can achieve the same thing in c++ using the new operator

Point* p = new Point();

But you have take care of the memory management yourself and use

delete p;

when you no longer need p or you'll have memory leak. Memory leak will also happen if an exception occurs before you free the memory so the example above is not recommended.

4

You declared an object of class type and supplied no initializer in the declaration. This means that if the class has a user-defined default constructor, then that default constructor is used to initialize the object. That's just how C++ works. The language specification says that it happens, so it happens.

I don't see what "null pointer" you are expecting to see and where. There are no pointers anywhere in your code.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Thats just what would happen if I did the exact same thing in Java, because I didn't explicitly call a constructor. – Czipperz Jun 28 '15 at 23:38
0

In instantiating objects,

In C++

MyClass obj;   //calls the default constructor. There can only be one.
               //either with empty parameter list or with args default.
MyClass obj(int,int); //constructor with non default args.

//equivalent of methods above
MyClass obj = MyClass() 
MyClass obj = MyClass(int,int);

//You can now have pointers
MyClass *obj = new MyClass();
MyClass *obj = new MyClass(int,int);

In Java

MyClass obj = new MyClass(); //or
MyClass obj = new MyClass(int,int);

Learn the basics of C++ well and dont confuse it with Java.

Acha Bill
  • 1,255
  • 1
  • 7
  • 20
0

in C++ A a default constructor is a default values assign in each objects or default values in each objects

Point(int x = 3, int y = 5); /// default constructor

so when you created an object without initializing some values the object will have default values coming from the default constructor

Kryssel Tillada
  • 180
  • 3
  • 13