0

I have a queue and I need to verify the input data type and handle the exception in case the data input isn't the same as the data type in the queue, how can I do this? MAIN.cpp

try {
    cout << "Insert character: ";
    cin >> ch;
    prova.push(ch);
} 
catch (wrong_insert& k) {
    k.allert();
};

This is the push function:

template <class t>
void queue<t>::push(const t& entry)
{
    if(*I need this condition*) throw wrong_insert();
    if(empty())
    {
        head_insert(front_ptr, entry);
        rear_ptr = front_ptr;
    }
    else
    {
        insert(rear_ptr, entry);
        rear_ptr = rear_ptr->link();
    }
    ++count;
    cout << "Inserted!" << endl;
}

and this is the exception class:

class wrong_insert
{
public:
    wrong_insert() : message("Wrong data inserted!"){};
    void allert(){ cout << message;};
private:
    string message;
};
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • http://stackoverflow.com/questions/351845/finding-the-type-of-an-object-in-c may help you – Ricky Mutschlechner Jul 13 '14 at 16:39
  • 4
    Input validation shouldn't equal exceptions, you should do the validation when parsing IMO. – Casper Beyer Jul 13 '14 at 16:40
  • @RickyMutschlechner no, it doesn't. RTTI (i.e. getting type information of a variable) has nothing to do with checking the semantics of user input. – Manu343726 Jul 13 '14 at 16:41
  • Like @CasperVonB said, why would this check be a runtime check instead of a compilation time check? Why do you have a templated `push` function on your `queue`? – Holt Jul 13 '14 at 16:42
  • @Manu343726 he did say data type, thought it would help. I may have misread. – Ricky Mutschlechner Jul 13 '14 at 16:43
  • I need to check the data type input and handle the exception in case it is different from the queue; so if I have a queue and put a character into "ch" to push in the queue, than the wrong_insert() exception must be thrown –  Jul 13 '14 at 16:45
  • 1
    C++ is statically and strongly typed. The only way to pass an object of a different type (without crazy casting, in which case you have nothing you can do) will be by passing a derived type. Since you store object by value (I presume), you'd create a new object of the correct type via slicing. So the check seems _very_ superfluous. – StoryTeller - Unslander Monica Jul 13 '14 at 16:45
  • 1
    Your use case is also pointless, since a char will be implicitly cast to an int during type promotion, so your function will only ever see an int. – StoryTeller - Unslander Monica Jul 13 '14 at 16:47
  • So there is no way I can do that? –  Jul 13 '14 at 17:04

2 Answers2

1

I would like to add this as a comment but it does not allow me to do so since i don't have 50 reputations yet.

I think if the type is wrong it will not compile in the first place.

Rukshan Perera
  • 150
  • 2
  • 9
0

If I understand your question right you are asking how you can check that no-one tries to insert say an integer into your queue of chars.

The answer is that you cannot, and need not do that at runtime. As C++ is a strongly typed language you will not be able to compile code where the data types are incompatible. Converting a char to an int is perfectly allowed and will not generate an error, but passing an int as a char may trigger an error/warning depending on the situation.

Note however, that you are in some cases allowed to pass an integer to a char if (a) asking specifically for it with a cast, or (b) if the compiler can know that the value of the integer is small enough to fit into a char. This happens at compile time. (NB Depending on your version of C++ such automatic conversions may give you either a warning or an error, so pay attention to your warnings.)

If you are planning to use this queue with classes that inherit from each other, your question becomes more relevant, as you are allowed to pass an object of a derived type as a reference or pointer of the base-class type. Here you can use the typeid operator, but know that that will trigger RTTI (RunTime Type Identification) and clutter up your executable with some extra code.

C++ is all about "pay for it only if you use it", and if you need it (and the performance doesn't suffer too much) use it.

Stian Svedenborg
  • 1,797
  • 11
  • 27
  • It's not *THAT* much extra code or space. One table per class and a call to a function pointer by indirection. The implicit `int` to `char` is news to me, should only work with `constexpr`. So very very rare that is. – StoryTeller - Unslander Monica Jul 13 '14 at 16:56
  • @StoryTeller Aye, that should only work with constexpr, and I think it also works with constants the compiler knows of, but I might be mistaken/implementation dependent on that one. – Stian Svedenborg Jul 13 '14 at 16:59
  • Okay, you are creating a false pretense here. It's not that much of a cost. Any type of support for dynamic dispatch will incur a cost, it's the price of programming OO to begin with. And c++ is quite low on the spectrum with the cost it does incur when it's asked to incur any at all. This backlash against C++ support for inheritance and overriding is so last century. – StoryTeller - Unslander Monica Jul 13 '14 at 17:01
  • Fair enough, I'll tone it down a bit ;) – Stian Svedenborg Jul 13 '14 at 17:59