এই আর্টিকেলে আমরা C++ STL-এ std::is_abstract টেমপ্লেটের কাজ, সিনট্যাক্স এবং উদাহরণ নিয়ে আলোচনা করব।
is_abstract টেমপ্লেটটি চেক করে যে ক্লাসটি একটি অ্যাবস্ট্র্যাক্ট ক্লাস কিনা।
অ্যাবস্ট্র্যাক্ট ক্লাস কী?
অ্যাবস্ট্র্যাক্ট ক্লাস হলো এমন একটি ক্লাস যাতে অন্তত একটি পিউর ভার্চুয়াল ফাংশন থাকে। আমরা অ্যাবস্ট্র্যাক্ট ক্লাস ব্যবহার করি কারণ যখন আমরা ফাংশন ডিফাইন করি, তখন আমরা তার ইমপ্লিমেন্টেশন জানি না, তাই এটি খুব কার্যকর হয় সেই ক্ষেত্রে যখন আমরা জানি না একটি ক্লাসের ফাংশন আরও কী করতে পারে কিন্তু আমরা নিশ্চিত যে আমাদের সিস্টেমে এমন একটি ফাংশন থাকতে হবে। তাই আমরা একটি পিউর ভার্চুয়াল ফাংশন ডিক্লেয়ার করি যা শুধু ডিক্লেয়ার করা থাকে এবং তার ইমপ্লিমেন্টেশন থাকে না।
সুতরাং, যখন আমরা ক্লাসের ইনস্ট্যান্স থেকে চেক করতে চাই যে ক্লাসটি একটি অ্যাবস্ট্র্যাক্ট ক্লাস কিনা, তখন আমরা is_abstract() ব্যবহার করি।
is_abstract() integral_constant- থেকে ইনহেরিট করা হয়েছে এবং true_type বা false_type দেয়, ক্লাস T এর ইনস্ট্যান্স পলিমরফিক ক্লাস টাইপ কিনা সেটার উপর নির্ভর করে।
সিনট্যাক্স
template <class T> struct is_abstract;
প্যারামিটারস
এই টেমপ্লেটে শুধুমাত্র একটি প্যারামিটার T থাকতে পারে, যা ক্লাস টাইপের, চেক করতে হবে যে ক্লাস T একটি অ্যাবস্ট্র্যাক্ট ক্লাস কিনা।
রিটার্ন ভ্যালু
এই ফাংশন বুল টাইপ মান রিটার্ন করে, true বা false।
এটি true রিটার্ন করে যদি T একটি অ্যাবস্ট্র্যাক্ট ক্লাস হয়, এবং false যদি T একটি অ্যাবস্ট্র্যাক্ট ক্লাস না হয়।
উদাহরণ
#include <iostream>
#include <type_traits>
using namespace std;
struct TP_1 {
int var;
};
struct TP_2 {
virtual void dummy() = 0;
};
class TP_3 : TP_2 { };
int main() {
cout << boolalpha;
cout << "checking for is_abstract: ";
cout << "\nstructure TP_1 with one variable :" << is_abstract<TP_1>::value;
cout << "\nstructure TP_2 with one virtual variable : " << is_abstract<TP_2>::value;
cout << "\nclass TP_3 which is derived from TP_2 structure : " << is_abstract<TP_3>::value;
return 0;
}
আউটপুট
checking for is_abstract: structure TP_1 with one variable : false structure TP_2 with one virtual variable : true class TP_3 which is derived from TP_2 structure : true
উদাহরণ
#include <iostream>
#include <type_traits>
using namespace std;
struct TP_1 {
virtual void dummy() = 0;
};
class TP_2 {
virtual void dummy() = 0;
};
struct TP_3 : TP_2 { };
int main() {
cout << boolalpha;
cout << "checking for is_abstract: ";
cout << "\nstructure TP_1 with one virtual function :" << is_abstract<TP_1>::value;
cout << "\nclass TP_2 with one virtual function : " << is_abstract<TP_2>::value;
cout << "\nstructure TP_3 which is derived from TP_2 class : " << is_abstract<TP_3>::value;
return 0;
}
আউটপুট
checking for is_abstract: structure TP_1 with one virtual function : true class TP_2 with one virtual function : true structure TP_3 which is derived from TP_2 class : true