I can't access field overridden in child class from parent C++ constructor and I can not use templates, because upstream project doesn't use them.
This is working Python prototype that I try to reimplement in C++. The code contains two driver classes - one child and one parent, and parent prints name
of the class during initialization.
class Driver(object):
name = "Unknown"
def __init__(self):
print(self.name)
class SpecificDriver(Driver):
name = "Specific"
def __init__(self):
super(SpecificDriver, self).__init__()
Driver()
SpecificDriver()
This prints two strings to console
Unknown
Specific
Looks like in C++ can't access overridden name
, because the name
doesn't exist at this point - Call child method from parent constructor. So maybe there is another way to get driver name
printed on initialization?
UPDATE (2018): The original title for this question was "Print overridden child field during initialization in C++ without templates" and it was closed as too broad.