4 Replies - 560 Views - Last Post: 05 February 2011 - 08:21 AM Rate Topic: -----

#1 demosthenes2k8  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 30-December 09

Pointer to Derived Function?

Posted 04 February 2011 - 01:18 PM

I was attempting to write some code to do an "interface" style class for a project I'm working on. Unfortunately, I realized that when you have a pointer to a base class, it calls the base class's function, even if the pointer points to a derived object and the derived class overloads the function.

Is there any way to have it call the derived class's function? I need to be able to store two different kinds of Derived classes by pointer, unless there's a better way.

class Base{
public:
	void foo() {
		cout << "Base";
	}
}

class Derived1 : public Base {
public:
	void foo() {
		cout << "Derived1";
	}
}

class Derived2 : public Base {
public:
	void foo() {
		cout << "Derived2";
	}
}

Base *obj;
Derived1 o1;
obj = &o2;
obj->foo(); // Can I have it call Derived1's foo()?



I need to store a bunch of objects of both Derived classes in the same "place"

This post has been edited by demosthenes2k8: 04 February 2011 - 01:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pointer to Derived Function?

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Pointer to Derived Function?

Posted 04 February 2011 - 01:22 PM

Create a pointer to the base class. Then assign a pointer of one the derived classes to that base class pointer. Then you can call any virtual functions that were defined in the derived class. That way, no matter which derived class the pointer holds, it will call the correct member function.
*EDIT*: you need to have virtual functions declared in the base class, and then those functions need to be defined in the derived classes for this to work. What you want to do is called polymorphism and the only way to do it is with virtual functions.

This post has been edited by vividexstance: 04 February 2011 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Pointer to Derived Function?

Posted 04 February 2011 - 01:27 PM

You need to read about Virtual functions.
Check this to get started.
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Pointer to Derived Function?

Posted 04 February 2011 - 01:28 PM

This does what you want:
// polymorphism-test.cpp
// Test of polymorphism
#include <iostream>
using namespace std;

class Base {
public:
    virtual void foo() = 0;  // pure virtual function
};

class Derived1 : public Base {
public:
	void foo() {
		cout << "Derived1\n";
	}
};

class Derived2 : public Base {
public:
	void foo() {
		cout << "Derived2\n";
	}
};

int main()
{
	Base *obj;
	Derived1 o1;
	obj = &o1;
	obj->foo(); // Can I have it call Derived1's foo()?
	Derived2 o2;
	obj = &o2;
	obj->foo();
}



Output:
Derived1
Derived2

This post has been edited by vividexstance: 04 February 2011 - 01:29 PM

Was This Post Helpful? 0
  • +
  • -

#5 demosthenes2k8  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 30-December 09

Re: Pointer to Derived Function?

Posted 05 February 2011 - 08:21 AM

Thank you!
One thing that I had forgotten was that I HAD used "virtual", so I'm not entirely sure why my code wasn't working. Unfortunately, I had to revert them so I could focus on another portion of the project, so I'll try to rewrite them, following this more closely.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1