typ eines variablen feststellen
-
hallo, ich will den typ eines variablen feststellen. ABER das geht nicht:
#include <iostream.h> main() { cin >> a; if (a = int) { cout << "a ist ein integer"; } else { cout << "a ist kein integer"; } }
-
nee so einfach geht das leider nicht
auch wird ein vergleich immer mir mit 2 '=' gemacht.in der hilfe findet man dazu folgendes :
typeid Operator
C++ Specific —>typeid( type-id )
typeid( expression )
The typeid operator allows the type of an object to be determined at run-time.
The result of a typeid expression is a const type_info&. The value is a reference to a type_info object that represents either the type-id or the type of the expression, depending on which form of typeid is used. See type_info Class for more information.
END C++ Specific
type_info Class
C++ Specific —>The type_info class describes type information generated within the program by the compiler. Objects of this class effectively store a pointer to a name for the type and an encoded value suitable for comparing two types for equality or collating order. The encoding rules and collating sequence for types are unspecified and may differ between programs.
class type_info {
public:
virtual ~type_info();
int operator==(const type_info& rhs) const;
int operator!=(const type_info& rhs) const;
int before(const type_info& rhs) const;
const char* name() const;
const char* raw_name() const;
private:
...
};
Note You must include the typeinfo.h header file in order to use the type_info class.END C++ Specific
viel spass beim experimentieren.
-
Versuchs mal mit typeid().
[ Dieser Beitrag wurde am 07.06.2003 um 09:14 Uhr von C Newbie editiert. ]
-
Ich glaub net das das klappt. Ich will ja nix falsches sagen, aber IMHO hat ein int keine typeid.
Zweitens wurde 'a' niemals definiert. Somit ist der versuch sowieso sinnlos.
-
sieht verdächtig trollig aus, er deklariert seine variable nicht
sonst wüßte er, von welchem typ sie ist..;) na ja...
-
ich deklariere die variable nicht, weil der typ noch nicht feststeht. erst nach der benutzer eingabe
ich hjab jetzt folgenends:
#include <iostream.h> main() { cin >> a; if (typeof(a) == int) { cout << "a ist ein integer"; } else { cout << "a ist kein integer"; } }
geht auch nicht
-
lol
du bist hier nicht in php
-
Ich will ja nix falsches sagen, aber IMHO hat ein int keine typeid.
Aber sicher doch.
Auch für int muss folgendes gelten:int i; assert(typeid(int) == typeid(i));
-
Original erstellt von <c++neuling>:
**ich deklariere die variable nicht, weil der typ noch nicht feststeht. erst nach der benutzer eingabeich hjab jetzt folgenends:
#include <iostream.h> main() { cin >> a; if (typeof(a) == int) { cout << "a ist ein integer"; } else { cout << "a ist kein integer"; } }
geht auch nicht :(**
Ja wie soll denn das gehen? Du hast a ja nicht deklariert, da kann es einfach nicht gehen.
Außerdem muss esint main()
heißen.
-
C++ ist Computernäher als du denkst
Der Computer muss den Speicher für die Variable ja schon vorher haben, damit du dann bei der eingabe da rein schreiben kannst.zur not lies in ein std::string ein und konvertiere nachher in eine zahl.
aber eine variable muss IMMER einen Typ haben. bevor sie verwendet wird