-3

While i was trying to put object type to same object type of array, I am not sure how to put this.

    Drink *a = new Drink("Whole Milk", 2.50, NEAT);
    c.drinkArray[c.drinkCount++] = a;

Can anyone help me? I am from java so I am so confused.....

  • 2
    Please provide a [mre]. – Yunnosch Oct 15 '21 at 04:03
  • I'm not completely sure what you're asking. Is it that you want to make an array with a custom constructor? If so, you can do `Drink *a = new Drink[size_of_array]("Whole Milk", 2.50, NEAT);` – Elliott Oct 15 '21 at 04:04
  • Please read [ask] and https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question . You need to tell us *what the problem is* - did something wrong happen when you tried your code? Show us *runnable* code that *has the same problem*, and *explain* that problem by telling us exactly what happened when you ran the code, exactly what is supposed to happen instead, and how that is different. – Karl Knechtel Oct 15 '21 at 04:12
  • @Elliott Thank you! so I guess * means the pointer? and is it how i should assign the class type value to class type array? – hellowldmaz Oct 15 '21 at 04:12
  • 1
    If the problem is that you can't figure out how to write the code - especially if you can't explain in plain English what the actual difficulty is or what needs to be done - consider abandoning your project and following a tutorial from start to finish. – Karl Knechtel Oct 15 '21 at 04:12
  • 1
    Note: unlike Java you don't need `new` to get an instance of a class. [Most of the time you're better off avoiding `new`](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). – user4581301 Oct 15 '21 at 04:20
  • 1
    My gut says that `drinkArray` is declared as `Drink[]` not as `Drink*[]`. You can't store a `Drink*` pointer in an array of `Drink` objects. – Remy Lebeau Oct 15 '21 at 04:49
  • What is the type of `c`? – jxh Oct 15 '21 at 04:53
  • There is a list of good C++ books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Don't assume that C++ is like Java just because of the similar punctuation and the spelling of some keywords. – molbdnilo Oct 15 '21 at 07:54

1 Answers1

0

As said try to avoid "new" in C++, I also tend to avoid 'C' style arrays (unless there is a clear benefit). 'C' style arrays don't have length checks and you can easily run into problems with writing out of bounds of the array and you will have to do some extra checking yourself.

#include <string>
#include <vector>

// not really sure what NEAT is 
// but at least it can be made typesafe like this
enum class DrinkAttribute
{
    UNKNWON,
    NEAT,
};

struct Drink
{
    std::string name = "no drink";
    double price = 0.0;
    DrinkAttribute attribute = DrinkAttribute::UNKNWON;
};

// put array and size information together including checks
struct Drinks
{
    void add(const Drink& drink)
    {
        if (number_of_drinks < max_number_of_drinks)
        {
            drinks[number_of_drinks] = drink;
            number_of_drinks++;
        }
        else
        {
            // some error handling here
        }
    }

    Drink& get(std::size_t n)
    {
        if (n < number_of_drinks)
        {
            return drinks[n];
        }
        else
        {
            // some error handling here
            // and return a drink representing no drink
            // so all control paths return a value
            // note I would probably just throw a std::invalid_argument exception here
            // but I consider exception handling to be out of the scope of the question.
            return no_drink;
        }
    }

    static const std::size_t max_number_of_drinks = 10;
    Drink drinks[max_number_of_drinks];
    Drink no_drink;
    std::size_t number_of_drinks = 0;
};

void ShowDrinksWithVector()
{
    // to make dynamic collections of something you're bettor of using std::vector
    // instead of arrays. This way you reuse tested code!
    std::vector<Drink> drinks;

    // at a drink to the collection of drinks
    // the 3 arguments passed to emplace_back are used
    // to construct an object of type Drink
    drinks.emplace_back("Whole Milk", 2.50, DrinkAttribute::NEAT);
}

void ShowDrinksWithArray()
{
    // this way you will have to write memory access checking code yourself
    // that's why you will end up with an extra class (or you have to resort
    // to using global variables)
    
    Drinks drinks;
    drinks.add(Drink{ "Whole Milk", 2.50, DrinkAttribute::NEAT });
    auto drink = drinks.get(0);
}

int main()
{
    ShowDrinksWithVector();
    ShowDrinksWithArray();
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19