Hi all,
I am using Visual Studio 2005, and developing some application. Currently i am facing one problem. I have written a class and declared some member variables in the class. In the constructor i am initializing the member variables to 0(member variable is int). But when i debugged the code i found that the variable is not getting initialized. It takes the garbage value and even though we assign 0 it is not getting changed.

class :- 
class foo
{
public:
    foo() {i = 0;}
    int  get() {return i;}
private:
    int i;
};

Can you please suggest why it is happening.

Edited 3 Years Ago by Dani: Formatting fixed

Hi all,
I am using Visual Studio 2005, and developing some application. Currently i am facing one problem. I have written a class and declared some member variables in the class. In the constructor i am initializing the member variables to 0(member variable is int). But when i debugged the code i found that the variable is not getting initialized. It takes the garbage value and even though we assign 0 it is not getting changed.

class :

class foo
{
public:
    foo() {i = 0;}
    int  get() {return i;}
private:
    int i;
};

Can you please suggest why it is happening.

But when i debugged the code i found that the variable is not getting initialized.
We can't prove or disprove your statement.
It seems that's the only possible reply...
Read http://catb.org/esr/faqs/smart-questions.html

Edited 3 Years Ago by mike_2000_17: Fixed formatting

>But when i debugged the code i found that the variable is not getting initialized.
We can't prove or disprove your statement.
It seems that's the only possible reply...
Read http://catb.org/esr/faqs/smart-questions.html

Hi Ark,
What i mean to say is that the garbage value which assigned to the variable was not getting replaced by Zero.

There are lots of reasons to get garbage value instead of desired zero or what else, for example:
- assignment to something via bad pointer(s)
- assignment to anything with out-of-range index
- array overwriting
- malloc instead of new (no constructor call)
and so on...
Well, try to trace your class variable from the start to the bad point...
Or set Data Breakpoint...
Or what else...
It's impossible to help with such info.
I have a trouble, help me...
What did you want to get? To gain sympathy? Yes, I'm very distressed...

Please, don't send all your codes. Probably you have used game libraries and other stuff which I don't want to install. Try to select only valued code fragments...
Your thread title is incorrect, of course. Class variables ARE initialized if their constructor(s) were called.

Let me allow to help this guy.

#include <iostream>

using namespace std;
class foo
{
    public:
       foo() {i = 0;}
       int get() {return i;}
     private:
       int i;
};

void main()
{
     foo p;     // no argument constructor will initialize i field.
     cout << endl << p.get();   // will print 0 (ZERO)
}

>Let me allow to help this guy
Are you sure that it was helpful post? ;)
1. no argument constructor called default constructor in C++
2. void main() is incorrect main signature in C++, never use it!

int main()

Well, try to help him/her. May be you have a crystal ball to see the code ;)

>Let me allow to help this guy
Are you sure that it was helpful post? ;)
1. no argument constructor called default constructor in C++
2. void main() is incorrect main signature in C++, never use it!

int main()

Well, try to help him/her. May be you have a crystal ball to see the code ;)

adatapost>I will follow.

Correct code after the suggestion of ArkM.

#include <iostream>

using namespace std;
class foo
{
    public:
       foo() {i = 0;}
       int get() {return i;}
     private:
       int i;
};

int  main()
{
     foo p;     // Default constructor will initialize i field.
     cout << endl << p.get();   // will print 0 (ZERO)
     return 0;
}

I had read about the constructor : Every class must have a constructor. If a class contains no instance constructor,a default instance constructor is automatically provided. That default constructor simply invokes the parameterless constructor of the direct base class(es).

If a class has a user defined constructor:
1. without argument - is called no argument constructor
2. parameterized constructor
3. copy constructor etc
will override the default constructor.

What is your suggestion?

ArKM said - Place your complete code.

Hi Ark,
I am sorry i can't place the source code. but its simmilar to the example which i have mentioned.

>...its simmilar to the example which i have mentioned
It's not an example of your code, it's a class definition with zero initialization of the member i...
Probably you have some kind of memory corruption. It's impossible to tell more without code fragments. Search an error in your code...

hi;
yous hould try using a default constructor. use a dummy variable to initialise the private data member or better still use a constructor initialiser list.
a code snippet of the fomer is given:
class foo
{
public:
foo(int a=0) {i = a;}
int get(){return i;}
private:
int i;
};

try it out in a main function . i have already...
AND IT WORKS!!!!

>AND IT WORKS!!!!
Of course, this funny constructor works. OP constructor works too. Even right (normal) constructor works:

class foo
{
public:
  foo():i(0) {}
  ...
};

;)
By the way, right constructor with default int parameter is:

class foo
{
public:
  explicit foo(int a = 0):i(a) {}
  ..

because no need in implicit conversion from int to foo...
But it's the other story.

This article has been dead for over six months. Start a new discussion instead.