Answer the question
In order to leave comments, you need to log in
How to create an instance of a class inside a macro or template, passing the name of the class if the class can/cannot have constructor parameters?
I have several classes with the same named methods that could not be made virtual.
It is necessary to bypass instances of all classes and call these methods.
It was possible to solve this with a macro, but crookedly - with the help of two macros, one for constructors without parameters, the other for constructors with parameters.
The template doesn't work better either.
Is this problem solvable in principle, or am I suffering in vain?
UPD Well, let's say there is such a conditional code:
// Example program
#include <iostream>
#include <string>
enum ClassNum {
cl1 = 0,
cl2 = 1,
};
class Parent{
public:
Parent(long addr): Addr(addr){}
bool PutData(int data) { *(int*)Addr = data; return (data == *(int*)Addr); }
//void NotVirtualF(...);
protected:
int c0 = 0;
int c1 = 50;
int c2 = 100;
int c3 = 150;
int c4 = 200;
private:
long Addr;
};
class Child0 : Parent{
public:
Child0() : Parent( (long)&(Parent::c0) ){};
void NotVirtualF() { PutData(42); PutData(24); PutData(37); }
};
class ChildNum : Parent{
public:
ChildNum(ClassNum num) : Parent( num == ClassNum::cl1 ? (long)&(Parent::c1) : (long)&(Parent::c2) ){};
void NotVirtualF() { PutData(88); PutData(77); }
};
class Child3 : Parent{
public:
Child3() : Parent( (long)&(Parent::c3) ){};
void NotVirtualF() { PutData(67); }
void NotVirtualF(bool zzz) { if(zzz) PutData(22); else PutData(71); }
};
class Child4 : Parent{
public:
Child4() : Parent( (long)&(Parent::c4) ){};
// No function at all
};
#define CHILD_MACRO(childName) { \
childName child##childName; \
child##childName.NotVirtualF(); \
std::cout << #childName "\n"; \
}
#define CHILD_MACRO_PARAM(childName, param) { \
childName child##childName##param(param); \
child##childName##param.NotVirtualF(); \
std::cout << #childName << param << "\n"; \
}
int main()
{
std::cout << "Hello\n";
int container = 5;
Parent P((long int)&container);
std::cout << (P.PutData(42) ? "OK" : "FAIL") << "\n";
CHILD_MACRO(Child0);
CHILD_MACRO_PARAM(ChildNum, cl1);
CHILD_MACRO_PARAM(ChildNum, cl2);
CHILD_MACRO(Child3);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question