views:

135

answers:

8

I know that default constructors initialize objects to their default values, but how do we view these values? If there's a variable of type int, it is supposed to be initialized to 0. But how do we actually view these default values of the constructors? Can anyone please provide a code snippet to demonstrate the same?

+2  A: 

Good coding practice: write your own constructor, so you know how it will be initialized. This is portable and guaranteed to always have the same behaviour. Your code will be easier to read and the compiler knows how to make that efficient, especially when using the special notation:

class Foo
{
  public:
    Foo() : i(0), j(0) {}

  private:
    int i;
    int j;
};
Benoit Thiery
+4  A: 

Unless specified otherwise, objects are constructed with their default constructor, only if one is available.

And for example ints are not initialized.

This is a common source of huge troubles and bugs, because it can have any value.

So the rule is , always initialise your variables, and for a class you do it in the initialization list

class A
{
private:
    int i;
    float f;
    char * pC;
    MyObjectType myObject;
public:
    A() :   // the initialisation list is after the :
    i(0),
    f(2.5),
    pC(NULL),
    myObject("parameter_for_special_constructor")  
    {}
}

}

Stephane Rolland
A: 

Default constructors do not automatically initialise ints to 0. You can use parentheses to indicate the default value though.

struct X
{
   int x;
};

struct X x1; // x1.x is not necessarily 0

struct Y
{
   int y;
   Y() : y()
   {
   }
};

struct Y y1; // y1.y will be 0
CashCow
+1 for being technically correct and voted down by some moron who didn't even leave an explanation. Just a nit: using the word `struct` in the variable declarations is unnecessary in C++. Cheers,
Alf P. Steinbach
I know the struct is not necessary at the point of variable declaration but I thought it would make the sample a bit clearer, the first case could be C anyway, the second one could not be as you cannot define constructors in C.
CashCow
MSalters
@MSalters: he he, actually C++ supports that reinterpret_cast, for C compatibility no doubt. It's in the classes section, start of (and flatly contradicting the section on reinterpret_cast). IIRC. Cheers,
Alf P. Steinbach
@Alf P. Steinbach: The cast is legal, yes. But that gives you a pointer rvalue. What can you do with that pointer? Is it even guaranteed to point to the uninitialized `x.x1`? Or does such a guarantee exist only if `x.x1` is initialized? It's in those subtle areas where C and C++ standards can unexpectedly differ.
MSalters
@MSalters: yes, it's formally guaranteed to point to `x.x1`, in C++. And so presumably also in C. From a practical point of view, the address of something does not depend on that something being initialized or not. And that would be absurd. I thought you were talking about other subtleties, like C tentative declarations, C implicit conversion to `void*`, the g++ silly-thing/bug about strict aliasing, that kind. Cheers,
Alf P. Steinbach
A: 

show your code

And if your int value is a member of class . you must give it a value in your default constructor func

MengMeng
+2  A: 

In C++, int is not a class and does not have a default (or any other) constructor.

An int is not guaranteed to be initialised to 0.

If you have a class that has an int as an attribute, you should explicitly initialise it in each of the class's constructors (not just the default one).

class sample
{
private:
    int x;
public:
    sample()
        :x(0)
    {
    }

    sample(const int n)
        :x(n)
    {
    }

    sample(const sample& other)
        :x(other.x)
    {
    }

    // [...]
};

This way you (and users of your class) can "view" the default values.

Johnsyweb
A: 

The default constructor is which can be invoked with 0 parameters. For example

struct X
{
   X(int x = 3) //default constructor
   {
     //...
   }
};

It initializes the object to whichever state you code it to initialize. However, if you don't define any constructor at all the compiler will attempt to generate a default constructor for you - which, in turn is equivalent to a constructor with no arguments and no body. That means that all the members of class/struct type will be initialized with their def. ctors and all the members of primitive types like int will remain uninitialized. Please note that I specialy noted that the compiler will attempt to generate a dflt ctor, because it can fail to do so, for example when one or more members of the class do not have default constructors. HTH

Armen Tsirunyan
...which will result in a compilation error (you may be understood as if the class will be left without a ctor at all)
davka
@davka: No, it will result in a compilation error if and only if you use your class in a context where a def.ctor(or any constructor) is required. Otherwise everything will be OK
Armen Tsirunyan
which is why I prefer the term `empty ctor` for a ctor with no parameters, reserving the `default ctor` to those generated by the compiler.
davka
@davka: "default constructor" is a term defined by the Holy Standard. It's defined as any constructor that can be called without arguments. It's an intricate philosophical question whether that permits a class to have two or more default constructors, since then there's no way to call any of them without arguments. Cheers,
Alf P. Steinbach
@Alf: yes, I know it's the standard terminology, I just find it somewhat confusing. But indeed, this is philosophy, not related to OP
davka
+1  A: 

AFAIK, if T is a type (not necessarily a class), T() returns a default value for the type T. Here's a small test I ran:

int main()
{
    char c = char();
    int i  = int();

    cout << "c = " << hex << (int) c << endl;
    cout << "i = " << i << endl;
}

The output is:

c = 0
i = 0
davka
Yeah, it's known as a **pseudo-constructor**. There's also a **pseudo-destructor**,but it can only be used on a named type (e.g. a typedef'd name), not directly on a built-in type. E.g. you can write `int()` but not `(42).~int()`. However, if you have a `typedef int Int;` then you can write `(42).~Int()`. Which helps in templated code where the code should work regardless of type. Cheers,
Alf P. Steinbach
A common trap though for a beginner is to type char c(); to try to initialise their character, and then they get compiler errors later because the above declares a function and does not define a variable.
CashCow
A: 

AS soon as you have created an object start printing the values such as ob.x. Thats the only way u can know what default constructor has assigned to the variables.

SPB