D
D
Dmitry Ivanov2018-02-09 16:43:14
C++ / C#
Dmitry Ivanov, 2018-02-09 16:43:14

How to make constructor overload without type extension?

We have two variations of the class constructor:

CAttribute(const string &name, const uint32_t index, const CSiiData &value);
CAttribute(const string &name, const bool hasIndex, const CSiiData &value);

When I try to declare a variable of this class, I get an error:
there is more than one instance of the "CAttribute::CAttribute" constructor corresponding to the list of arguments:
the function "CAttribute::CAttribute(const std::string &name, uint32_t index, const std::string &value)"
the function "CAttribute::CAttribute(const std ::string &name, bool hasIndex, const std::string &value)"
argument types: (const char [12], int, const char [5])

This is due to the expansion of the bool type to int .
The essence of the class in a nutshell: storage of the "Attribute" object, which contains the name, value and can be part of the "array", both numbered and unnumbered. For example:
  • "Simple" attribute:
    attr: value

  • "Numbered" attributes:
    attr[0]: some_value
    attr[1]: some_value2

  • "Unnumbered" attributes:
    attr[]: some_value
    attr[]: some_value2

Question : is it still possible to create constructors with the specified set of arguments?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Taratin, 2018-02-09
@IDma88

I would leave in general one view constructor
Negative indexes are treated as "Unnumbered" attributes.

A
Alexander Movchan, 2018-02-09
@Alexander1705

This happens due to the expansion of the bool type to int

This is not true. intcan be reduced both to booland to uint32_t. And the compiler doesn't know what exactly you mean. It is enough to explicitly cast the argument to the desired type so that you can unambiguously determine which constructor you are calling.
CAttribute("name", (uint32_t)index, "value");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question