Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7906483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:59:28+00:00 2026-06-03T10:59:28+00:00

I have 2 classes, A and B. B inherits from A. //C++ class A

  • 0

I have 2 classes, A and B. B inherits from A.

//C++    
class A
{
    public:
        int getA() {return this->a;};
        A() {this->a = 42;}
    private:
        int a;

};

class B: public A
{
    public:
       B() {this->b = 111;};
       int getB() {return this->b;};
    private:
        int b;

};

Now I would like to interface those two classes using Cython and have the possibility to call the getA() method from a B instance:

a = PyA()
b = PyB()
assert a.getA() == b.getA()

Currently my pyx file looks like this:

cdef extern from "Inherit.h" :
    cdef cppclass A:
       int getA()

    cdef cppclass B(A):
       int getB()


cdef class PyA:
    cdef A* thisptr

    def __cinit__(self):
       print "in A: allocating thisptr"
       self.thisptr = new A()
    def __dealloc__(self):
       if self.thisptr:
           print "in A: deallocating thisptr"
           del self.thisptr

    def getA(self):
       return self.thisptr.getA()

cdef class PyB(PyA):
    def __cinit__(self):
       if self.thisptr:
          print "in B: deallocating old A"
          del self.thisptr
       print "in B: creating new b"
       self.thisptr = new B()

    def __dealloc__(self):
       if self.thisptr:
           print "in B: deallocating thisptr"
           del self.thisptr
           self.thisptr = <A*>0

    def getB(self):
       return (<B*>self.thisptr).getB()

While I hope this code is not doing anything too dangerous, I also hope there is a better way to handle it.

Also using the module generates the following output:

>>> from inherit import *
>>> b = PyB()
in A: allocating thisptr
in B: deallocating old A
in B: creating new b
>>> b.getA()
42
>>> b.getB()
111
>>> del b
in B: deallocating thisptr

And I don’t really like allocating an A instance just to free it immediately after.

Any advice about how to do it correctly ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:59:39+00:00Added an answer on June 3, 2026 at 10:59 am

    I make some experiments, and have quite ready answer but now i see where is the problem:

    If your extension type has a base type, the __cinit__ method of the
    base type is automatically called before your __cinit__ method is
    called; you cannot explicitly call the inherited __cinit__ method.

    So the real problem is that Cython types still do not have constructors, only pre initializer hook __cinit__ which behave more like default constructors. You cannot call virtual method from constructor and you cannot call it from __cinit__ either (if you make a call, it behave like non virtual).

    Somehow inside __cinit__ the type(self) return correct type object, but it is useless. Cython do not have static fields, methods and type object can be only instance of type (no metaclasses). Python @staticmethod is easy overridable, so it is useless.

    So there is no other way like to put allocation inside def __init__(self):, and check for initialized thisptr wherever you use it.

    You may consider creating a global dummy C++ object, and assign it to thisptr to avoid checking and crashing. There are no post initializer hook, so you will be unable to check if correct initialization already have taken place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two classes, one inherits from the other Class A{ void
Note: This is a homework assignment. I have two classes, one inherits from the
I have a base class that inherits List<> which has multiple classes derived from
I have defined an Event class: Event and all the following classes inherit from
I have a bas class called Media with two classes that inherit from it,
I have a MustInherit Parent class with two Child classes which Inherit from the
I have two classes one of which inherits from the other. Im trying to
This is for a web project so i have several classes that inherit from
I have an interface, and some classes that inherit from it. public interface IFoo
Let's say I have a base class Animal from which a class Cow inherits,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.