0
\$\begingroup\$

I'm writing an adjacency matrix, in this moment I'm trying to figure out how to indicate a default numeric value for an unassigned edge. There is the macro _I64_MAX but it is said that it's a bad practice to use macros. I wish to know if there's other way to assign such value (as a keyword as in the case of nullptr instead of NULL) or if what I'm doing is OK.

Why don't I use 0? Because the distance for an edge which start and end vertex is the same is: $$e=(v_i,v_i);~~~||e||=0$$

AdjacencyMatrix::AdjacencyMatrix(const SinglyLinkedList &vertices) : vertices_(vertices)
{
    this->edge_matrix_ = new int64_t *[this->vertices_.Size()];
    for (size_t i = 0; i < this->vertices_.Size(); i++)
    {
        this->edge_matrix_[i] = new int64_t[this->vertices_.Size()];
        for (size_t j = 0; j < this->vertices_.Size(); j++)
        {
            this->edge_matrix_[i][j] = _I64_MAX;
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ This is the wrong site for this question. Maybe a mod will move it to stack-overflow or something like that. \$\endgroup\$ – Martin York Jul 16 at 21:51
  • 1
    \$\begingroup\$ You may find this page interesting: en.cppreference.com/w/cpp/types/numeric_limits \$\endgroup\$ – Martin York Jul 16 at 21:52
  • \$\begingroup\$ No because it is related to best practices, at first I doubted but I read the faq \$\endgroup\$ – Miguel Avila Jul 16 at 21:53
  • 3
    \$\begingroup\$ It's OK to ask "Does this code follow common best practices?", but not "What is the best practice regarding X?" \$\endgroup\$ – Martin York Jul 16 at 21:57
1
\$\begingroup\$

Use of macros

In this case, the macro you using is from a library header that was included with your compiler. This header (ultimately limits.h, although <climits> is usually used with C++ code) is also used by C code, so C++ specific language features cannot be used. These are OK to use.

The C++ way of referencing a minimum value for a type is to make use of std::numeric_limits (in the <limits>) header. In your case, you can replace _I64_MAX with std::numeric_limits<int64_t>::min().

Other notes

All of the references to this-> are unnecessary and can be removed from your code. You rarely need to specify this in C++ code (there a few specific cases where it is needed, like for variable name disambiguation or calling a function thru a member function pointer).

The for loop might be able to be rewritten using iterators or the range-for style if this is supported by the SinglyLinkedList type.

| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.