aharon
aharon

Reputation: 7623

c++ constructors

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?

Upvotes: 3

Views: 314

Answers (8)

ereOn
ereOn

Reputation: 55726

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).

Upvotes: 5

andand
andand

Reputation: 17487

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; 
};

Upvotes: 0

Darel
Darel

Reputation: 630

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) {}

Upvotes: 1

DevSolo
DevSolo

Reputation: 697

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.

Upvotes: 1

chrisbtoo
chrisbtoo

Reputation: 1572

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.

Upvotes: 1

On Freund
On Freund

Reputation: 4436

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

Upvotes: 2

Justin Ethier
Justin Ethier

Reputation: 134167

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

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

Upvotes: 0

James McNellis
James McNellis

Reputation: 354969

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) { }

Upvotes: 10

Related Questions