#include <iostream>

class base
{
	public:
		void print() { std::cout << "base" << std::endl; }
};

template< class B >
class der : public B
{
  int i;

	public:
		virtual void print() 
		{
			std::cout << (B *)this << std::endl; B::print(); 
		}
};

int main(int, char **)
{
	der< der< base > > inst;
	inst.print();
}

output :

0xbf93de8
0xbf93de8
base

Any ideas why this prints the same pointer values?

Recommended Answers

All 3 Replies

Why not? It's the address of the same object called inst.

Nevermind.. I was thinking this would be different in the base class as opposed to the derived.

this is why I was confused:

class a { int i; };
class b { int i; };

class c : public a, public b
{
  void print() { printf("%p, %p\n", (a *)this, (b *)this); }
};

prints different values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.