Home > Back-end >  Is there any way to detect whether a class is the base class
Is there any way to detect whether a class is the base class

Time:10-30

For example is similar to the following code
 
The class People {
};
The class Student: public People {
};

Void printPeopleInfo (const People * performance, int len);


If I design a SDK, printPeopleInfo () this function is used to print class People, but if the following call
 
Student Student []=ten Student {/* */};
PrintPeopleInfo (students, 10);

Compiled no problem, but running stage is likely to appear problem, so I hope in printPeopleInfo () function testing, incoming parameters is People, if not (such as incoming class inherits from People), I hope to compile stage can error, is there any way to achieve?

CodePudding user response:

People define a virtual function in the parent class, student subclass rewritten, this method is used to return to the current name of the class, convenient detection,

CodePudding user response:

Article 3 of the more effective c + + do not use polymorphism of array, you not that kind of idea!

CodePudding user response:

Can use template
 # include & lt; iostream> 
#include
Class A, {}.

Class B: A {};

Class C {};

Int main ()
{
STD: : cout & lt; & lt; STD: : boolalpha;
STD: : cout & lt; & lt; "A2b:" & lt; & lt; STD: : is_base_of & lt; A, B> : : value & lt; & lt; '\ n';
STD: : cout & lt; & lt; "B2a:" & lt; & lt; STD: : is_base_of & lt; B, A> : : value & lt; & lt; '\ n';
STD: : cout & lt; & lt; "C2b:" & lt; & lt; STD: : is_base_of & lt; C, B> : : value & lt; & lt; '\ n';
STD: : cout & lt; & lt; "Same type:" & lt; & lt; STD: : is_base_of & lt; C, C> : : value & lt; & lt; '\ n';
}

CodePudding user response:

Why can't add as you do,
In an array, the array subscript access element is according to the element size, calculation of the offset array [I] is equivalent to * (array + I * sizeof (T)), when the derived class arrays to a base class pointer to visit again, because of the derived class and a base class size, problems lead to offset,
 
The class CBase
{
Public:
CBase () {}
Virtual ~ CBase () {}
};

The class CDrived: public CBase
{
Public:
CDrived () {}
~ CDrived () {}

Private:
Int m_nData;
};

Void FuncArray (const CBase arr [], int nCount)
{
STD: : cout & lt; & lt; & Arr [1] <& lt; STD: : endl;
}

Int main ()
{
CDrived arr [3].
STD: : cout & lt; & lt; & Arr [1] <& lt; STD: : endl;
FuncArray (arr, 3);

system("pause");

return 0;
}

Results:

You can see the output of two addresses are not the same

CodePudding user response:

reference 1st floor xky96 response:
people define a virtual function in the parent class, student subclass rewritten, this method is used to return to the current name of the class, convenient detection,

What if

refer to the second floor SleekStone response:
more effective c + + article 3 do not use polymorphism to the array, you not that kind of idea!

I realized that this problem is to look at this item, I design the SDK, I want others to use, if the incoming base class, I can in the compile phase error function

CodePudding user response:

reference 3 floor truth is right or wrong response:
can use template
 # include & lt; iostream> 
#include
Class A, {}.

Class B: A {};

Class C {};

Int main ()
{
STD: : cout & lt; & lt; STD: : boolalpha;
STD: : cout & lt; & lt; "A2b:" & lt; & lt; STD: : is_base_of & lt; A, B> : : value & lt; & lt; '\ n';
STD: : cout & lt; & lt; "B2a:" & lt; & lt; STD: : is_base_of & lt; B, A> : : value & lt; & lt; '\ n';
STD: : cout & lt; & lt; "C2b:" & lt; & lt; STD: : is_base_of & lt; C, B> : : value & lt; & lt; '\ n';
STD: : cout & lt; & lt; "Same type:" & lt; & lt; STD: : is_base_of & lt; C, C> : : value & lt; & lt; '\ n';
}

Thank you. I'll try

CodePudding user response:

Some language is operator can directly use to judge, but c + + does not support the
And
Void printPeopleInfo (const People * performance, int len);
Argument is a pointer to the type of People, * performance within the function could not judge the actual type
In addition, is_base_of only for type, rather than a variable, or a type a

CodePudding user response:

A, a1.
B, b1;
STD: : cout & lt; & lt; STD: : boolalpha;
STD: : cout & lt; & lt; "A2b:" & lt; & lt; STD: : is_base_of & lt; The decltype (a1), the decltype (b1) & gt; : : value & lt; & lt; '\ n';
  • Related