C ++ Primer study note _ 102 _ special tools and technology, primer_102

Source: Internet
Author: User

C ++ Primer study note _ 102 _ special tools and technology, primer_102
Special tools and technologies-runtime type Identification [continued]

 

Iii. Use of RTTI

When comparing two derived class objects, we want to compare data members that may be specific to the derived class. if the parameter is referenced by a base class, you can only compare the members that appear in the base class. We cannot access the members that appear in the derived class but not in the base class.

Therefore, we can use RTTI to return false when trying to compare different types of objects ).

We will define a single equal operator. Each class defines a virtual function equal, which first forcibly converts the operands to the correct type. If the conversion is successful, a real comparison is performed. If the conversion fails, the equal operation returns false.

 

1. class hierarchy

class Base{    friend bool operator==(const Base &,const Base &);public:    // interface member for baseprivate:    virtual bool equal(const Base &) const;};class Derived : public Base{    friend bool operator==(const Derived &,const Derived &);public:    // interface member for baseprivate:    virtual bool equal(const Base &) const;};


2. Type-sensitive equal Operators

bool operator==(const Base &lhs,const Base &rhs){    return typeid(lhs) == typeid(rhs) && lhs.equal(rhs);}

 

3. virtual function equal

Each class in the hierarchy must define its own equal version. The equal function in the derived class will start in the same way: forcibly convert real parameters into the data type of the class itself.

Bool Derived: equal (const Base & rhs) const {// You must convert it to the pointer type, for the reason, refer to the previous C ++ Primer blog if (const Derived * dp = dynamic_cast <const Derived *> (& rhs )) {// compare two Derived objects and return result} return false ;}

 

4. Basic equal functions

bool Base::equal(const Base &rhs) const{    // compare two Base objects and return result}

You do not need to forcibly convert the parameters before using them. * this and the parameters are both Base objects. Therefore, all operations on the object are defined for the parameter type.

 

Iv. type_info class

Operations supported by type_info

T1 = t2

If the t1 and t2 objects are of the same type, true is returned. Otherwise, false is returned.

T1! = T2

T1! = T2 returns true if the t1 and t2 types of the two objects are different. Otherwise, false is returned.

T. name ()

T. name () returns the C-style string, which is the printable version of the type name. Type names are generated using system-related methods

T1.before (t2)

Returns the bool value indicating whether t1 exists before t2. The order of before force is related to the compiler.

 

Because it is intended to be used as a base class, the type_info class also provides public virtual destructor. If the compiler wants to provide additional type information, it should be carried out in the derived class of type_info.

The default constructor, the copy constructor, and the value assignment operator are both defined as private. Therefore, objects of the type_info type cannot be defined or copied. The only way to create a type_info object in the program is to use the typeid operator.

The name function returns a C-style string for the type name indicated by the type_info object. The value used for a given type depends on the compiler. Specifically, it does not need to match the type name used in the program. The only guarantee for the return value of name is that it returns a unique string for each type. However, you can still use the name Member to display the name of the type_info object:

    int iobj;    cout << typeid(iobj).name() << endl;    cout << typeid(8.16).name() << endl;    cout << typeid(std::string).name() << endl;    cout << typeid(Base).name() << endl;    cout << typeid(Derived).name() << endl;


The G ++ compiler displays:


Note: The format and value returned by name change with the compiler. The type_info class changes with the compiler. Some additional member functions provided by the compiler provide additional information about the types used in the program.

// P653 exercise 18.20 class A {}; class B: public A {}; class C: public B {}; int main () {A * pa = new C (); cout <typeid (* pa ). name () <endl; // 1A cout <typeid (pa ). name () <endl; // P1A C cobj; A & ra = cobj; cout <typeid (& ra ). name () <endl; // P1A cout <typeid (ra ). name () <endl; // 1A B * px = new B; A & raTest = * px; cout <typeid (raTest ). name () <endl; // 1A}


C programming language and C Primer Plus which is better?

The former is only an entry-level book. If you want to use C entry-level programming, you can read this book and learn C ++ or JAVA in the future. C primer plus is the most comprehensive .. If you want to develop in C, you can see that he is also a tool book. If you want to study C in depth, you need to read more books at the bottom of C, such as C compiler implementation. C language Assembly knowledge

After learning c primer plus, what should I learn?

I have read a lot of books from the author. The advantage is that the knowledge is wide and the disadvantage is that it is easy to be refined. I personally strongly suggest:
Clear goals-clear programming language-select a good book for good reading, good programming (manual programming is the focus)-select a lot of books (the same programming language) as a tool book-start to achieve the goal

In addition, if you only want to practice applications (such as Enterprise Portal and office platform websites), Learning data structures is of little use. The application algorithm has already been saved to the relevant class library. If you are not interested in it (Love coding algorithm, curious ...), The help is really not too great.
Assembly language .. First, it is not practical for window design .. It is hard to understand and use. If you do not want to do embedded-related things, you really don't need it. If you are really interested, the current compilers all come with disassembly, you can have a look at it.

As an enterprise portal and an Office Platform website, php, mysql, and HTML must be used. You can use dreamweaver, wamp, and other environments, I used "elaborate on php" when I first studied it. As for HTML, there are entry-level documents everywhere on the Internet, which are basically used for the interface of the artist. dreamweaver is very simple.

Besides php, java is also a good choice. Although I don't know it, java is still very powerful in website maintenance and so on. I don't know it, but I can only make one proposal.

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.