The sizeof operator and the typeid operator _c language in C + + programming

Source: Internet
Author: User
Tags static class

sizeof operator
produces the operand size associated with the size of the char type.
Grammar

sizeof unary-expression sizeof (type-name)

Note
The result of the sizeof operator is the size_t type, which is the containing file stddef. The integer type defined in H. With this operator, you can avoid specifying the computer-dependent data size in your program.
The sizeof operand can be one of the following:
The type name. To use sizeof with a type name, the name must be enclosed in parentheses.
An expression. When used for an expression, you can specify sizeof, whether or not you use parentheses. does not evaluate an expression.
When the sizeof operator is applied to an object of type char, it generates 1. When the sizeof operator is applied to an array, it produces the total number of bytes in the array, not the size of the pointer represented by the array identifier. To get the size of the pointer represented by an array identifier, pass it as a parameter to a function that uses sizeof. For example:

#include <iostream>
using namespace std;

size_t getptrsize (char *ptr)
{return
  sizeof (PTR);
}

int main ()
{
  char szhello[] = "Hello, world!";

  cout << "The size of a char is:"
     << sizeof (char)
     << "\nthe length of" << Szhello  ;< "is:"
     << sizeof Szhello
     << "\nthe size to the pointer is"
     << getptrsize (Szhello ) << Endl;
}

Sample output

The size of a char is:1
the length of Hello, world! is:14 the size of the
pointer is 4

When the sizeof operator is applied to the class, struct, or union type, the result is the number of bytes in the object of that type, and any padding added to align the member data on the word boundary. The result does not necessarily correspond to the size calculated by adding the storage requirements of each member. /zp compiler options and pack pragma affect the alignment bounds of a member.
The sizeof operator never produces 0, even for empty classes.
The sizeof operator cannot be used for the following operands:
Function. (However, sizeof can be applied to pointers to functions.) )
Bit fields.
A class that is not defined.
void type.
The dynamically allocated array.
An external array.
Incomplete type.
The name of the incomplete type with parentheses.
When the sizeof operator is applied to a reference, the result is the same as when the sizeof applied to the object itself.
If an indeterminate array is the last element of the structure, the sizeof operator returns the size of the structure without the array.
The sizeof operator is typically used to evaluate the number of elements in an array by using an expression in the following form:
sizeof Array/sizeof Array[0]

typeID operator
Grammar

   typeID ( 
   type-id
    )
typeid (expression)
(expression)

The

Notes
typeID operator allows you to determine the type of an object at run time. The result of the
typeID is the const type_info&. The value is a reference to a Type_info object that represents a type of type-id or expression, depending on the form of the typeid being used. The
typeID operator does not apply to managed types (abstract declarations or instances). The
typeID operator performs a run-time check when applied to the left value of a polymorphic class type, where the actual type of the object cannot be determined by the provided static information. In this case, a
reference to a class
uses a pointer to dereference the
with the following pointer (that is, []). (Note that it is generally not safe to use subscripts with pointers to polymorphic types.)
If expression points to a base class type, but the object is actually a type derived from the base class, the Type_info reference of the derived class is the result. Expression must point to a polymorphic type (a class with a virtual function). Otherwise, the result is the type_info of the static class referenced in expression. In addition, you must dereference the pointer to use the object it points to. If the reference pointer is not canceled, the result will be the type_info of the pointer, not the content it points to. For example,

Expre_typeid_operator.cpp
//compile with:/gr/ehsc
#include <iostream>
#include <typeinfo.h >

class Base {public
:
  virtual void Vvfunc () {}
};

Class Derived:public Base {};

using namespace std;
int main () {
  derived* pd = new Derived;
  base* PB = pd;
  cout << typeid (pb). Name () << Endl;  Prints "class Base *"
  cout << typeid (*PB). Name () << Endl;  Prints "class Derived"
  cout << typeid (PD). Name () << Endl;  Prints "class Derived *"
  cout << typeid (*PD). Name () << Endl;  Prints "class Derived"
  delete pd;
}

If expression is dereferencing a pointer and the value of the pointer is zero, typeid throws a Bad_typeid exception. If the pointer does not point to a valid object, a __non_rtti_object exception is thrown to indicate that an attempt was made to parse the RTTI that raised an error, such as an access violation, because the object was in some way invalid (invalid pointers or code not compiled with/GR).
If expression is neither a pointer nor a reference to the object's base class, the result is a type_info reference that represents the static type of the expression. The static type of the expression will reference the type of the expression known at compile time. Execution semantics are ignored when evaluating the static type of an expression. In addition, references, if possible, are ignored when determining the static type of an expression:

Expre_typeid_operator_2.cpp
#include <typeinfo>

int main ()
{
  typeid (int) = = typeID (int &); Evaluates to True
}
typeID can also be used in a template to determine the type of template parameters:
//Expre_typeid_operator_3.cpp
//compile with:/C
#include <typeinfo>
template < typename T > 
t max (t arg1, t arg2) {
  cout << typeid (T). Name () << "s compared." << Endl;
  Return (Arg1 > Arg2 arg1:arg2);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.