Abstract Classes and Private / Public Methods

I wish to create a class to compile with the Panda3D engine. I notice that the sample file I am looking at(basicSkel.cxx and .h) has all of the methods under PUBLISHED. I wrote my own class that looks like this:

#ifndef MYCLASS_H
#define MYCLASS_H

#include "pandabase.h"
#include <string>

class MyClass {

  string getString();

  void set_value_alt(int n);
  int  get_value_alt(); 

	string PubStr();

	string PrivStr();
  int _value;



The public and private methods that I have defined cannot be called. I’m guessing it’s because they are not ‘PUBLISHED’. I am unsure of what ‘PUBLISHED’ means and I am trying to write classes in C++ that will take advantage of abstract classes using virtual methods, pure virtual methods, private methods, protected methods, and public methods. I’m sure there is a way for me to get this to work but I am lost as to how to use it within Panda3D because I cannot use anything that is not ‘PUBLISHED’ it seems.

I think PUBLISHED is related to “Interrogate” (C++ to Python code porting utility). If you don’t want to port your code in Python I think you don’t need PUBLISHED at all (i’m not sure).

I think you are right but the problem is that I will need to call some of my C++ methods with Python.

“published” is one step higher than “public”: it means the method is published, and available, to Python. It’s so public that it’s visible outside of C++.

You can’t publish a protected method. Even if you could, your subclasses wouldn’t inherit it. Although you can subclass a C++ class in Python, you can’t overload any of the C++ methods that way, even if they are declared as virtual methods. The whole C++/Python interface layer is not quite that magical.

If you really want to write a hybrid C++/Python class, you need a bit more instruction than what we provide here. It’s a complicated thing, and I suspect it’s more than you want to get into. I’ll be happy to provide guidance, though, if you tell me you really do want to walk that dark path.

My advice, though, would be to design your classes so that overloading in Python is not part of the design. Use a has-a, rather than an is-a, relationship. Write the core functionality you need in C++, then write a true Python class that interfaces to that core functionality, without inheriting from it. You can then subclass from your Python class in the normal way. The Rope.py / ropeNode.h system is a good example of this: we have written the core functionality in the C++ RopeNode class, and then written a separate Rope.py Python class that uses that functionality without inheritance. If someone wanted to, they could subclass Rope.py without surprises.


What i really want to do is use C++ for path finding with the A* algorithm and just instantiate some classes and call their methods from python. I’m not trying to have a Python class inherit from a C++ class if that’s what you are thinking.

I was just looking for a way to make a C++ class abstract. I am rusty with C++ but i thought all I had to do was declare a method in the class as a pure virtual method and then the class would become abstract and could not instantiated. I also thought this would force all derived classes to implement those pure virtual methods. This is not the behavior I am seeing though. I am able to compile a derived class that does not implement any of the base class’s pure virtual functions.

A pure virtual method declaration looks like this:

  virtual int  get_value_alt()=0;

Note the “=0” on the end. This is what makes it a “pure” virtual method, as opposed to an ordinary virtual method. A pure virtual method has no body declared anywhere. This is not related to ordinary virtual methods (the “virtual” keyword), which is just one that stores its pointer in the class definition so that an inheriting class can overload the method and the bottommost derivation will be called regardless of the pointer type. (In Python, all methods are like C++ virtual methods.)

This is also unrelated to published, public, protected, and private. You can certainly have a pure virtual method that is published. When you call this method from Python, it will call the bottommost derivation.

As you describe, if you have any undefined pure virtual methods, you cannot instantiate the class.


Thanks for your help. I think I know how to continue with what I am doing. I have another problem now though. I set breakpoints in my C++ files. Calling the code with the breakpoints from within python works and my debugger stops the code there but I cannot see the values of various variables within that specific code block. Any ideas?

For that to work well, you have to be compiled in Debug mode rather than Release mode. That’s a whole other can of worms, though, because it means everything has to be compiled in Debug mode, including all of Panda, and all of Python too, and all of the third-party tools!

If you’re prepared to recompile the entire world, then go for it, and that will solve your problems. If you don’t care to go through that much trouble, then I suggest you learn to live with the limitations of the debugger. Use cerr statements, for instance, to print out your values at runtime, rather than querying them in the debugger.