3

i wrote this code:

class A {
  public:
    A(){d=2.2;cout<<d;}
    A(double d):d(d){cout<<d;}
    double getD(){return d;}

  private:
    double d;
};

class Bing  {
  public:
    Bing(){a=A(5.3);}
    void f(){cout<<a.getD();}
  private:
    A a;
};

int main() {
  Bing b;
  b.f();
}

i get the output: 2.2 5.3 5.3 instead of 5.3 5.3. it's something in the constructor.... why am i getting this? how can i fix it?

aharon
  • 7,393
  • 10
  • 38
  • 49
  • Next time please format your code using the `101010` button on the edit page. – sbi Jun 17 '10 at 13:48
  • 2
    3 basic questions in 10 minutes... maybe it would be more effective to read some well-written book ? – ereOn Jun 17 '10 at 13:52
  • no! i just need to answer a couple questions for my friend. i rarely use c++... – aharon Jun 17 '10 at 13:55
  • 2
    @aharont: Perhaps your friend could use one of the introductory books from [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – James McNellis Jun 17 '10 at 13:57
  • 1
    @aharont: Just out of curiosity: Why can't your friends ask for themselves ? – ereOn Jun 17 '10 at 14:12
  • he don't want to, he is pretty despaired from all this programming thing, and i help him learn this staff... – aharon Jun 17 '10 at 14:18
  • @aharont Actually, your friend would probably learn more things if he read the answers himself. No offense, but it is unlikely that you can teach him well enough all the things you just learnt. – ereOn Jun 17 '10 at 17:24

8 Answers8

10

Your class A has two constructors: a default constructor, which sets d to 2.2, and a constructor taking a double, which sets d to whatever you pass into the constructor.

You have a member variable of type A in your class Bing. This member variable is initialized before the body of the Bing constructor is entered. Since you don't list the Bing member in the constructor's initializer list, its default constructor is called. You can explicitly initialize it with the desired value by initializing it in the initializer list:

Bing() : a(5.3) { }
James McNellis
  • 348,265
  • 75
  • 913
  • 977
5

That's because you didn't use the initialization list.

Your Bing constructor should be like this:

Bing() : a(5.3)
{
}

In your previous code, you create a new instance of A (calling the "default constructor") then affect it to another variable of type A (which indeed call the assignement operator).

ereOn
  • 53,676
  • 39
  • 161
  • 238
  • I don't see A's copy constructor used anywhere. I'm not sure whether you mean to refer to the assignment operator, or the constructor that takes a double. – dave4420 Jun 17 '10 at 14:03
  • @Dave Hinton, you're absolutely right. I obviously meant the "assignement operator". Thanks for pointing that out. – ereOn Jun 17 '10 at 14:11
2

Because you're not Initializing a in the initialization list, so A's default constructor is called.

On Freund
  • 4,376
  • 2
  • 23
  • 30
1

Change:

    Bing(){a=A(5.3);}

To:

    Bing():a(5.3){}

and you'll get your expected result.

The runtime is initializing a with the default constructor, and then assigning your value to it. In my form, it's initializing with your value.

chrisbtoo
  • 1,572
  • 9
  • 13
1

Think of it this way, you can assign and construct objects in C++. I'm paraphrasing a little, so cut me some slack, language lawyers. :)

All objects need construction, but not everyone needs assignment. C++ allows us to double duty and pass values via the constructor. In this case, you're constructing and assigning in one step, as the others suggest you do.

If you just construct an object, you'll get the default value(s) for your object when default c-tor is called followed by value(s) after an assignment.

DevSolo
  • 667
  • 4
  • 9
1

The default constructor for A a is still being executed in the constructor for Bing. Even though you are initializing a immediately in the body of the Bing constructor, the default constructor for a has already executed at that point.

However, using the initialization list for Bing will prevent the default constructor for a from executing. Change the default constructor of Bing to:

Bing(): a(5.3) {}
Darel
  • 640
  • 1
  • 7
  • 26
0

It prints 2.2 because you are calling A's constructor from Bing's:

Bing(){a=A(5.3);}
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
0

As others have mentioned, you should initialize elements of A and Bing in the respective contructor initialization lists. You can also set A::d to have a default value, so you don't need two constructors in A. That is, you have:

class A { 
  public: 
    A(){d=2.2;cout<<d;} 
    A(double d):d(d){cout<<d;} 
    double getD(){return d;} 

  private: 
    double d; 
};

You could rewrite it as:

class A { 
  public: 
    A(double d=2.2) : d(d) { cout << d; } 
    double getD() { return d; } 

  private: 
    double d; 
};
andand
  • 17,134
  • 11
  • 53
  • 79