complicated class question

This is a discussion on complicated class question within the C++ Programming forums, part of the General Programming Boards category; I want to be able to call virtual function (vfunc) in two differents classes (one derived in another).. for instance: ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    complicated class question

    I want to be able to call virtual function (vfunc) in two differents classes (one derived in another)..

    for instance:

    Code:
    class derived {
    public:
    	derived() { }
    	void run() { vfunc(); }
    	
    	virtual void vfunc() { }
    };
    
    class someclass : public derived {
    public:
    	someclass() { }
    	void vfunc() { std::cout << "someclass virtual function called\n"; }
    };
    
    
    class baseclass : public someclass, public derived {
    public:
    	baseclass() { }
    	void vfunc() { std::cout << "baseclass virtual function called\n": }
    };
    
    void main() {
    	baseclass test;
    	test.run(); //will print baseclass virtual..
    	//how to call vfunc that is inside someclass with test.run();
    	//is this possible?
    }

    In other words (I know it sounds complicated):
    I want to be able to print "baseclass virtual.." and "someclass virtual.." from baseclass (test) by calling run.
    The reasons I want to do this is i'm working on my thread class that will be derived in other classes with virtual functions, and they will
    be derived into main class which will also have the same virtual function (thread class is used to create thread and call virtual function) and
    I want to keep all data in the same class (main class with derived classes) so I wouldnt have to pass the reference to main thread to the other (worker) threads/classes.

    I hope you know what I mean.. Thanks for help

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    I don't know what do you want.
    Here what you can get with easy
    Code:
    #include <iostream>
    
    class base {
    public:
    	base() { }
    	void run() { vfunc(); }
    	
    	virtual void vfunc() {std::cout << "base virtual function called\n"; }
    };
    
    class someclass : public base {
    public:
    	someclass() { }
    	virtual void vfunc() { std::cout << "someclass virtual function called\n"; }
    };
    
    
    class derived : public someclass{
    public:
    	derived() { }
    	virtual void vfunc() { std::cout << "derived virtual function called\n"; }
    };
    
    int main(void) {
    	base test;
    	test.run(); //will print baseclass virtual..
    	someclass test2;
    	test2.run();
    	derived test3;
    	test3.run();
    	return 0;
    }
    And that is the output:
    Code:
    base virtual function called
    someclass virtual function called
    derived virtual function called
    If you want some other output - could you explain the difference?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    How to call vfunc in someclass from derived?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    you can make it not virtual
    Code:
    #include <iostream>
    
    class base {
    public:
    	base() { }
    	void run() { vfunc(); }
    	
    	void vfunc() {std::cout << "base vfunc function called\n"; }
    };
    
    class someclass : public base {
    public:
    	someclass() { }
    	void vfunc() { std::cout << "someclass vfunc function called\n"; }
    };
    
    
    class derived : public someclass{
    public:
    	derived() { }
    	void vfunc() { std::cout << "derived vfunc function called\n"; }
    	void vfunc2(){someclass::vfunc();}
    	void vfunc3(){base::vfunc();}
    };
    
    int main(void) {
    	derived test3;
    	test3.vfunc();
    	test3.vfunc2();
    	test3.vfunc3();
    	return 0;
    }
    Output
    Code:
    derived vfunc function called
    someclass vfunc function called
    base vfunc function called
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    I know that but I want to call it virtual.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    One question, why are you inheriting derived twice for someclass? There's no need. The functionality is already in baseclass from someclass.


    Code:
    class baseclass : public someclass
    {
    public:
    	baseclass(){}
    
    	void vfunc( void ) 
    	{
    		someclass::vfunc();
    	}
    };
    I'm sure there's another way of doing it which I've forgotten

  7. #7
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Seems like I figured out (happy):

    Code:
    struct A {
    	void run() { f(); }
    	virtual void f() { cout << "A::f()" << endl; };
    };
    
    struct B : A {
    	void f() { cout << "B::f()" << endl;};
    };
    
    struct C : A {
    	void f() { cout << "C::f()" << endl;};
    };
    
    struct D : B, C { };
    
    int main() {
    	D d;
    
    	B *bp = &d;
    	C *cp = &d;
    
    	bp->run();
    	cp->run();
    	return 0;
    }
    C *cp = &d;
    cp->run();

    Whats the other way to do this instead with pointers?

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    struct D : B, C { };
    I wonder why you want to have A twice in D ?
    google "deadly diamond of death"
    Kurt
    Last edited by ZuK; 12-22-2006 at 11:45 AM.

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    BTW: a more common way to do "that" is
    Code:
    #include <iostream>
    using namespace std;
    
    struct A {
    	void run() { f(); }
    	virtual void f() { cout << "A::f()" << endl; };
    };
    
    struct B : A {
    	void f() { cout << "B::f()" << endl;};
    };
    
    struct C : A {
    	void f() { cout << "C::f()" << endl;};
    };
    
    int main() {
            A * bp = new B;
    	A * cp = new C;
    
    	bp->run();
    	cp->run();
           
            delete bp;
            delete cp;
    	return 0;
    }

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Taking your scenario at face value (a D object with two embedded A objects from different inheritance paths), the way to do it would be this:
    Code:
    int main() {
    	D d;
    
    	d.B::run();
    	d.C::run();
    	return 0;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    What if I want to have more than one copy of some data members from the top level class, but only one copy of the others (in A class/struct)?

    I have to separate base to base class and virtual base class?
    How should I do this?

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    By splitting the class into parts and inheriting from one virtually and the other non-virtually. But the whole thing sounds fishy to me. Too complex. Try to simplify your design.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21