Isfaaq
Isfaaq

Reputation: 435

Implementation of array attribute with C++ class

EDIT

I am learning C++ through a series of questions from the university I attend. Here is an excerpt of the problem in question:

A retailer shop wishes to store information about its sales. Typical information it wishes to store about a sale are: Sale identification number (a unique number for each sale), customer’s surname and other names as well as the customer’s address. A sale can consist of up to 10 different items, with different quantities of each item. For each item, the sale class should store the item number, item description, unit cost and the number of units of the items purchased as well as the total cost for the item.


TLDR: I am trying to write a constructor for a class which contains an array as one of its attribute.

Background

I am learning about Classes in C++ and am currently trying out interface.

The Code

For the sake of clarity, here is a simplified version of the interface showing only the constructor:

Sale.h

class Sale {
    int* products_id[];

    public:
    Sale(int& products_id[]);
};

Now, the interface needs to be implemented:

Sale.cpp

#include "Sale.h"
Sale::Sale(int& product_id[]) {
    this->product_id[] = product_id[];
};

The Problem

  1. For some reasons, my IDE (VS Code) is complaining about an error

    no suitable conversion function from "product_id" to "product_id *" exists

  2. I am unable to find any relevant resources online.

I would appreciate it if someone could help me out or at least point me in the right direction. Regards.

Upvotes: 0

Views: 2443

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 149025

I shall start here at your first sentence:

I am trying to write a constructor for a class which contains an array as one of its attribute.

This is brave... Arrays are not first class elements of the language, and are often left hidden in standard containers. A general rule in C++ is: if you have no strong reason to use a raw array in C++, just stick to containers. Exceptions are:

  • interfacing with legacy or C code
  • implementation of custom containers
  • low level optimization
  • and of course learning...

Problems with arrays that are solved with containers:

  • the size of an array is determined at compilation time => use a std::vector here
  • you cannot directly assign arrays but only process them element wise => std::array is enough here
  • they silently decay to pointers when used in expressions, so when you pass an array to a function, the callee only receives a pointer to the first element and has no way to guess the size
  • dynamic raw arrays are hard to manage because the class will only contain a raw pointer, and you will have the ownership problem: one and only one free per malloc

So a class that contains an array as one of its attributes should not be an ordinary common class. It should either be a container with the copy/move/destructor problem (search for rule of 3, rule or 5). Or the size must be constant for coordinates for example.

Upvotes: 1

tigertang
tigertang

Reputation: 457

First part: Pointer and C-Array

I guess you learned Java before writing such code snippet.

In Java, we have the handlers (or pointers to some extent) to each object, but in C++ we transmit objects directly by copying (without reference notation or redefinition of operator=). Therefore, the Java-style Type arr[] is misused in your code, and it has totally different meanings according to C/C++ contexts:

  1. Flexible array member which is only available in C99
  2. Just a pointer (not array) in function parameters
  3. Initializing an array without specifying the length directly
  4. Maybe more...

For instance:

#include <iostream>
using std::cout, std::endl;
int calc_sum(int arr[], int len) { // pointer
    int ans = 0;
    for (int i = 0; i < len; i++) {
        ans += arr[i];
    }
    return ans;
}
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(int);
    cout << n << endl;
    cout << calc_sum(arr, n) << endl;
    return 0;
}

Second part: Reference

When you write code like int a = 3; int &b = a;, you intended to make b an alias for a which means "now a and b are same". Therefore int& products_id[] means a pointer to the reference of int. And I think that is not what you want.

Upvotes: 1

Related Questions