Why isn't my class constructor initializing its member variables?

Eddie

Forgive me if the information is a bit lacking as I am trying to follow the recommended "Asking Guidelines".

I have created a class called "Item" to store item information and have defined my constructor as follows:

Item::Item(std::string number, std::string name, int quantity, double price) {
    itemNumber = number;
    itemName = name;
    quantity = quantity;
    unitPrice = price;
}

and I initialize it in my main function like so:

Item temp("string", "string", 0, 0);

but when I printed the values of item, only itemNumber and itemName printed correctly.all that printed was random garbage. Then I realized the issue is quantity and price aren't getting initialized. Why is that?

πάντα ῥεῖ

Try

this->quantity = quantity; 

or (IMHO better):

Item::Item(const std::string& number, const std::string& name, int quantity_, double price) {
                                                          // ^
    // ...
    quantity = quantity_;
    // ...
}

or (IMHO even better) use a constructor initializer list:

Item::Item(const std::string& number, const std::string name&, int quantity, double price) 
: itemNumber(number), itemName(name), quantity(quantity), unitPrice(price) {}

" Why is that?"

The parameter name currently shadows your member variable name in the constructor's body scope.


Personally I tend to have class declarations like follows

class Item {
public:
    Item(const std::string& number, const std::string& name, int quantity, double price) 
    : itemNumber_(number)
    , itemName_(name)
    , quantity_(quantity)
    , unitPrice_(price) {}
private:
    std::string itemNumber_;
    std::string itemName_;
    int quantity_;
    double unitPrice_ 
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Why isn't $destroy triggered when I call element.remove?

来自分类Dev

Why won't my HTML page print according to my CSS document?

来自分类Dev

Why my app-template is not taking effect on all its children in XSLT?

来自分类Dev

Alcatraz plugin isn't installing?

来自分类Dev

Why isn't this jQuery firing on click? (w/ radio buttons)

来自分类Dev

Why don't my points display in Haskell OpenGL?

来自分类Dev

Why isn't this simple REGEX working?

来自分类Dev

Why isn't overflow: hidden working?

来自分类Dev

How to make wrapper class forward its constructor arguments to std::vector's constructor?

来自分类Dev

Why isn't read_csv() parsing my dates?

来自分类Dev

Why won't ">> " print at the end of my printf statement?

来自分类Dev

Why is one of my base class constructors deleted? (C++11)

来自分类Dev

Why an instance of inherited class can't access to protected member of base class in different package

来自分类Dev

why doesn't my c# base constructor get called?

来自分类Dev

Why xor operation on constants (character) is working differently when used variables in its place

来自分类Dev

Why are my different instance variables linking together in python

来自分类Dev

Why won't my simple angularjs work?

来自分类Dev

Why my SeekBar isn't updating?

来自分类Dev

Why isn't this multiline Java pattern matching?

来自分类Dev

Why isn't ngAnimate working on a div?

来自分类Dev

Circular dependency in the class constructor

来自分类Dev

Converting a pointer to a struct to its first member

来自分类Dev

Why do I need another constructor in an extended abstract class?

来自分类Dev

Why doesn't Perl see my file in the working Windows directory?

来自分类Dev

Angular2 DI - initializing multiple different instances in the same constructor

来自分类Dev

How to extend a class with a different constructor?

来自分类Dev

Why isn't the C# compiler able to cast a literal negative value to an enum?

来自分类Dev

Why was copy constructor called in this example?

来自分类Dev

Why won't my init.d service start?

Related 相关文章

  1. 1

    Why isn't $destroy triggered when I call element.remove?

  2. 2

    Why won't my HTML page print according to my CSS document?

  3. 3

    Why my app-template is not taking effect on all its children in XSLT?

  4. 4

    Alcatraz plugin isn't installing?

  5. 5

    Why isn't this jQuery firing on click? (w/ radio buttons)

  6. 6

    Why don't my points display in Haskell OpenGL?

  7. 7

    Why isn't this simple REGEX working?

  8. 8

    Why isn't overflow: hidden working?

  9. 9

    How to make wrapper class forward its constructor arguments to std::vector's constructor?

  10. 10

    Why isn't read_csv() parsing my dates?

  11. 11

    Why won't ">> " print at the end of my printf statement?

  12. 12

    Why is one of my base class constructors deleted? (C++11)

  13. 13

    Why an instance of inherited class can't access to protected member of base class in different package

  14. 14

    why doesn't my c# base constructor get called?

  15. 15

    Why xor operation on constants (character) is working differently when used variables in its place

  16. 16

    Why are my different instance variables linking together in python

  17. 17

    Why won't my simple angularjs work?

  18. 18

    Why my SeekBar isn't updating?

  19. 19

    Why isn't this multiline Java pattern matching?

  20. 20

    Why isn't ngAnimate working on a div?

  21. 21

    Circular dependency in the class constructor

  22. 22

    Converting a pointer to a struct to its first member

  23. 23

    Why do I need another constructor in an extended abstract class?

  24. 24

    Why doesn't Perl see my file in the working Windows directory?

  25. 25

    Angular2 DI - initializing multiple different instances in the same constructor

  26. 26

    How to extend a class with a different constructor?

  27. 27

    Why isn't the C# compiler able to cast a literal negative value to an enum?

  28. 28

    Why was copy constructor called in this example?

  29. 29

    Why won't my init.d service start?

热门标签

归档