Constructor needs reviewing, not sure if I did this right.

Hello,

I am very very very new to C++. I am trying to figure out if I am completing the Huge-integer assignment correctly. For this assignment I must create a class that has the ability to hold an arbitrary number of digits using the the C++ STL class std::vector to do so. I have tried to watch multiple youtube videos and read online to understand the basics of this assignment. I am feeling very hopeless because I am not sure if, I am doing anything correct. Any advice or guidance on my code so far would be great. I have only written the constructors thus far because I am not certain if I am using vector correctly.


Last edited on
For this assignment I must create a class that has the ability to hold an arbitrary number of digits using the the C++ STL class std::vector to do so.

Normally when one talks about "digits" I would expect to see a "character" based data, not ints.

I have only written the constructors thus far because I am not certain if I am using vector correctly.

HugeInteger() create a constructor and set it to 0.
HugeInteger( const HugeInteger & other) - Copy constructor make a deep copy of other.

Do you realize that by default a std::vector is empty? So if all you're using is a std::vector to hold the "digits" there is no reason for creating a constructor, the default compiler provided constructor should be enough.

Also if you're just using a std::vector there is no need for a copy constructor. The compiler supplied copy constructor should be adequate since a std::vector is trivially copied no need to do a deep copy.

Your constructor is called when an object of your class is created, before that time, there was no object, so nothing could have happened to the vector in your class.

I find it strange to itterate over a vector and to print every value in it (either integer or char like jlb suggested) to the console in the constructor. Before you place a value in the vector using push_back (like you commented out) the vector will be empty and your for-loop will not do anything.

I can imagine that having such a for-loop to print out the content of the vector can be desirable, but then I would expect it to be in a separate function inside your class.

Also, currently your vector is a public variable in the class. Is that intentional? It might be, but personally I would prefer a private vector with some functions to access it.

Kind regards, Nico

p.s.: If you are using "namespace std;" you don't have to copy the std:: part of std::vector
Last edited on
Topic archived. No new replies allowed.