Explicit and implicit class declarations¶
Even if a class has no explicit constructor, pygccxml
will provide
a constructor declaration. This is due to CastXML
and GCC-XML
generating
implicit constructors (for example copy constructors) in their XML output.
The same thing holds for assignment operators and destructors.
To be able to discriminate between the different types of declarations,
the decl.is_artificial
attribute can be used.
Let’s consider the following c++ file (example.hpp):
namespace ns{
class Test {
public:
Test(); // This is the constructor
};
}
In this example, the constructor is explicitly defined. The declaration tree will contain two constructors. The first one is the one we defined explicitly, and is not marked as artificial. The second one is the copy constructor, which was implicitly added, and is marked as artificial.
from pygccxml import utils
from pygccxml import declarations
from pygccxml import parser
# Find out the c++ parser
generator_path, generator_name = utils.find_xml_generator()
# Configure the xml generator
xml_generator_config = parser.xml_generator_configuration_t(
xml_generator_path=generator_path,
xml_generator=generator_name)
# The c++ file we want to parse
filename = "example.hpp"
decls = parser.parse([filename], xml_generator_config)
global_namespace = declarations.get_global_namespace(decls)
ns = global_namespace.namespace("ns")
# We have just one declaration in ns, which is our Test class:
classTest = ns.declarations[0]
print(classTest.name, type(classTest))
# > 'Test', <class 'pygccxml.declarations.class_declaration.class_t'
# Loop over the two constructors:
for constructor in classTest.constructors():
print(str(constructor), constructor.is_artificial)
# > ns::Test::Test() [constructor], False
# > ns::Test::Test(ns::Test const & arg0) [constructor], True