12 Replies - 819 Views - Last Post: 01 July 2010 - 05:08 PM Rate Topic: -----

#1 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Quick Question

Posted 01 July 2010 - 02:54 PM

Can I overload a pure virtual function ?


class base
{
public:
virtual void print() =0;
};

class derived : public base
{
public:
virtual void print(int n); //err?
};


Is This A Good Question/Topic? 0
  • +

Replies To: Quick Question

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Quick Question

Posted 01 July 2010 - 03:08 PM

You can provide an overloaded function, but you still need to provide an implementation of the original...
Was This Post Helpful? 1
  • +
  • -

#3 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Quick Question

Posted 01 July 2010 - 03:11 PM

I think I see what you're saying, because overloading doesn't work between two different scopes.. But, can I do something like this:


class derived : public base
{
public:
using base::print;
virtual void print(int n);//ok?
};


Was This Post Helpful? 0
  • +
  • -

#4 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Quick Question

Posted 01 July 2010 - 03:14 PM

From memory the function must have a body so the virtual function in the base class takes on the function in the derived class.

If you are wishing to pass an integer variable to the function the virtual must also prototype this... Eg.

So
class base {
public:
   virtual void print(int) = 0;
};

class derived : public base {
public:
	void print(int n);
};

void derived::print(int n) {
	printf("printf function value = %i\n", n);
}


Once you have one pure virtual the base class becomes abstract and subsequently you cannot instantiate it, below i extended on this a little.

#include <stdio.h>
#include <stdlib.h>

class base {
public:
   virtual void print(int) = 0;
	void saySomething();
};

void base::saySomething() {
	printf("Virtual Classes and Functions\n");
}

class derived : public base {
public:
	void print(int n);
};

void derived::print(int n) {
	printf("printf function value = %i\n", n);
}

int main() {
	derived dev;
	dev.print(2);
	dev.saySomething();
	return 0;
}


Hope this helps :)

This post has been edited by DaneAU: 01 July 2010 - 03:18 PM

Was This Post Helpful? 1
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Quick Question

Posted 01 July 2010 - 03:19 PM

I don't see a scope issue at all.

Your base class indicates a pure virtual function called print that takes 0 arguments. It follows then that all derived classes implement such a function. That is, I should be able to write: derive d* = ...; d->print(); There must be a valid print() implementation. You have no such implementation. Yes, you have a function with the same name, but it's a different function...

It is possible to have a scope issue. But that's not your situation.
Was This Post Helpful? 0
  • +
  • -

#6 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Quick Question

Posted 01 July 2010 - 03:21 PM

Okay, thanks.

If that being the case, anyone know a great way to do this:


//class using boost::asio to spawn a thread.
class Thread_Base
{
private:
//thread pointer goes here.
public:
virtual void run()=0; //<-- problem
};

class Task : public Thread_Base
{
public:
//functions with public interface
//that require different arguments
//say...
void PrintSomething( int n, bool B)/>;

virtual void run(); //?? spawning a thread
//via polymorphism using functions with different interfaces?
};

class TaskTwo : public Thread_Base
{
//lots of these..
};




The answer is probably in the boost library somewhere, a better way to do this probably does exist..

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#7 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Quick Question

Posted 01 July 2010 - 03:28 PM

Any class that has Thread_base as a base class in your program will need to implement the run() method in each class definition....

So you have the base class
class Thread_Base
{
private:
//thread pointer goes here.
public:
virtual void run()=0; //<-- problem
};



Now you have,
class Task : public Thread_Base
{
public:
//functions with public interface
//that require different arguments
//say...
void PrintSomething( int n, bool B)/>;

virtual void run(); //?? spawning a thread
//via polymorphism using functions with different interfaces?
};



Which doesn't implement the run method that is a pure virtual function, so if you are wishing to create a multitude of classes using Thread_Base as a base class, each of them must contain a run method with the same signature as the base class ( Thread_Base) 's virtual run method.


class Anything : public Thread_Base {
public:
void run();
void PrintSomething( int n, bool B)/>;
};

void Anything::run() {
// code //
}

void Anything::PrintSomething(int n, bool B)/> {
   printf("Anything Class");
}

// another class
class Anything2 : public Thread_Base {
public:
void run();
}

void Anything2::run() {
  // code //
}

// etc..



These derived classes can of course have many other member functions specific to their role...

This post has been edited by DaneAU: 01 July 2010 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Quick Question

Posted 01 July 2010 - 03:30 PM

Last post didn't help.

--edit--
A container that holds any other type (or at least a pointer to one), which can be set by the derived class creator manually, and implemented properly in the run() function will work, but there is likely a better solution.

ex.
class Thread_Base
{
public:
vector<void*> v;//set with req. data before using.

virtual void run()
{
//use v[0] v[1] etc.
}
};


This post has been edited by taylorc8: 01 July 2010 - 03:34 PM

Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Quick Question

Posted 01 July 2010 - 03:33 PM

I should mention that it is possible to have a derived abstract class. That is:

struct Base
{
    virtual void f() =0;
};

struct Derived : Base
{
    void g() { // ... }
};

struct DerivedDerived : Derived
{
    void f() { // ... }
};



And only DerivedDerived is a concrete type.

taylorc8, you're going to have expend a bit more effort in describing your problem here. Telling us our posts aren't helping, and nothing more, is not useful. You're the one asking for help, so you might want to make your question clearer...
Was This Post Helpful? 0
  • +
  • -

#10 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Quick Question

Posted 01 July 2010 - 03:42 PM

Well, I'm about to abandon the idea, but it's fairly simple, forgive me for not explaining properly.


void ExecuteTask( Task_Base *tb)
{
tb->run();
}

int main()
{
Task_Base *one = new TaskOne;
Task_Base *two = new TaskTwo;

ExecuteTask(one);
ExecuteTask(two);
}



The problem is, the classes i'm deriving from the base class, "Task_Base" require some arguments that I can't fit into the run() properly.

I guess the solution is incredibly simple, and involves adding the required data to the class like this:


class TaskOne : public Task_Base
{
//data stored here:
public:
int n;
string s;

virtual void run()
{
cout << n;
cout << s;
}
};



If you don't know this already, I'm an idiot.


int main()
{
Task_Base *p = new TaskOne;
p.n = 100;
ExecuteTask(p);
return 0;
}



And.. I just solved my own mildly retarded problem...

It all starts with a quick question, and no short answer.. :(

This post has been edited by taylorc8: 01 July 2010 - 03:47 PM

Was This Post Helpful? 0
  • +
  • -

#11 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Quick Question

Posted 01 July 2010 - 04:34 PM

Yep it usually does start with a seemingly simple question, once you delve into it you realise that there is more to it than you initially realised.

just remember once you declare a pure virtual in a class, that class becomes abstract - subsequently it cannot be instantiated directly. The whole idea of inheritance is to help store information in structures, so you should keep common member variables encapsulated in the base class and any derived class specific variables are contained with in the derived class with their respective mutator and accessor functions.

This post has been edited by DaneAU: 01 July 2010 - 04:35 PM

Was This Post Helpful? 0
  • +
  • -

#12 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Quick Question

Posted 01 July 2010 - 04:42 PM

No. I understand, how that works. ^^^^^
Was This Post Helpful? 0
  • +
  • -

#13 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Quick Question

Posted 01 July 2010 - 05:08 PM

woops misread your response, thought you didn't understand - but you do :)

This post has been edited by DaneAU: 01 July 2010 - 05:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1