Abstract base classes use and abstract base class pointers as template container elements

Source: Internet
Author: User
//abstractclass.cpp:Defines the entry point for the console application.//#include "stdafx.h" #i


Nclude <vector> using namespace std;


Class A {public: <span style= "White-space:pre" > </span>virtual void f () = 0;}; Class B:public A {public: <span style= "White-space:pre" > </span>void f () <span style= "White-space:pre" &G	T


</span>{<span style= "White-space:pre" > </span>}; int _tmain (int argc, _tchar* argv[]) {<span style= "white-space:pre" > </span>vector<a *> VEC (10);//We don't
You can declare an abstract base class object, but you can declare a pointer to an abstract base class object <span style= "White-space:pre" > </span>vec[0] = new B; <span style= "White-space:pre" > </span>vec[0]->f ()//to a call to an abstract base class pure virtual function, executing a member function of the derived class with the same name <span style= "
White-space:pre "> </span>delete vec[0];
<span style= "White-space:pre" > </span>return 0; }


//abstractbasepointerastemplatecontainerelement.cpp:Defines the entry point for the console
Application.

#include "stdafx.h"//template container template <class t> class Vector {public:t m[10];

Abstract base class A {public:virtual void f () = 0;};

Derived class class B:public A {public:void f () {}};
	int _tmain (int argc, _tchar* argv[]) {//pointer to abstract base class as element of template container vector<a *> vec;
	Dynamically creates an object of a derived class, pointing to a *p = (A *) new B by a pointer to an abstract base class;
	Join the pointer to the abstract base class that points to the derived class object vec.m[0] = p;
	...//The pointer to the abstract base class of the derived class object out of Team a *q = vec.m[0];
	At this point, the call to the abstract base class pure virtual function is converted to the invocation of the derived class member function q->f ();
	Dynamically releasing the object delete Q of the derived class;
return 0; }

Remark I: Variables are initialized, in particular the pointers need to be initialized, and the abstract base class pointers in the template container are no exception: Vector<a *> VEC (1);

Vec[0] = NULL;
	Remark II: So, for example, for a pointer linked list with a leading node, remember to initialize the header node to null.
		Linklist ()//constructor {m_head = new char *;

M_head->data = null;//head node ...} Remark III: However, if the pointer list is a template list with pointers as elements, then the operation of the initialization of the short node should not be performed in the constructor of the template list, linklist ()//constructor {m_head = new node<t>
		;
	memset (M_head->data, NULL, sizeof (T)); Do not assign an element directly to the constructor of the template base class! Because this will empty the header node of the other class-linked list with the class that has its own constructor as an element.
		For example, an initial string class with 1 elements (' "): string ()//constructor {m_p = new char[1];
		M_len = 1;

M_p[0] = ' = '////initial with 1 elements (' ")} in this template linked list as a template, define a string-class as the element of a string list, then the chain header node its member m_len,m_p will be a value of 0.
		Remark IV: The correct way to do this is that when the template list is initialized, the right node polymorphism is assigned to a template type of data: linklist ()//constructor {m_head = new node<t>; M_head->data = T ();//template type} in which the assignment operation of the template type is actually implied in the composite copy constructor of the node class.
	So there is a more concise way, is to omit the template type explicit assignment, using the new operator, implicitly and polymorphic call template type constructors, the first node to initialize: Linklist ()//(recommended) constructor {M_head = new node<t>;

If you define a string list at this point, the header node can also have 1 elements (' "). Remark V: However, it is still necessary to note that if the template type of the template list is a pointer, because the pointer itself is not initialized automatically, you need to remember to manually initialize its head node when defining a template linked to a pointer element, or to define an inherited class constructor to initialize the pointer after inheriting the template list.
	Linklist<char *> linklist;
 Linklist.m_head->data = null;//Header node pointer assignment is NULL

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.