calling a method without instantiating a class

Hello

I have a C++ Abstract class (NClass) of which a method f1 is being directly called. f1 is allocating memory (and its causing a memory leak).

Here's a representation of the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Nclass : public Zclass {
public:
	explicit Nclass();
	Nclass(const Nclass& a);
	virtual ~Nclass() {};

	virtual void f1(...); //this allocates memory for a rel and puts it on h_rel
	virtual bool f2(...)=0;
	
private:
	Heap<rel> h_rel;
};


Pclass::f1(...)
{
...
Nclass::f1();
...
}


My questions are:

1. How can Pclass::f1() directly call Nclass::f1 without instantiating an Nclass explicitly?
2. I set a breakpoint in gdb on Nclass::~Nclass and it was not hit - is that expected?
3. It looks like a new NClass is being generated each time Pclass::f1() is called (I set a breakpoint in gdb and saw that the "this pointer" changes. Where is the new NClass being deallocated.

thanks!
¿what is the relationship between Pclass and Nclass?

2. ¿did you destroy (implicit or explicit) any Nclass object?
Question #1 - thanks - that is likely a clue that I did not notice.

1
2
3
4
5
6
7
8
9
10
11
12
class Pclass : public Nclass
{
...
virtual void f1(...); // same signature as Nclass::f1
...
}

Pclass::f1(...)
{
...
Nclass::f1();
}



#2 - I didn't see any explicit or implicit call to delete Nclass.
Last edited on
¿how do you create the objects?
1
2
3
4
5
6
{
   Pclass *ptr = new Pclass; //bad
} //memory leak
{
   Pclass obj;
} //destructor call implicit 



1_ Pclass is an Nclass. Line 11 is calling the member function as defined in the parent class
3_ Nope, you are not creating another Nclass objects. As for this changing, show how did you measure that.
OK, thanks so much - its a bit convulated bit ultimately there is a new without a delete.
As for #3, this "creation of NClass" was just my bad understanding of some gdb output.
Topic archived. No new replies allowed.