Virtual functions help

This is a discussion on Virtual functions help within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble creating a B1 reference to d1. I get a compiler error saying "error C2243: 'type cast' : ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Virtual functions help

    I'm having trouble creating a B1 reference to d1. I get a compiler error saying "error C2243: 'type cast' : conversion from 'D1 *' to 'B1 &' exists, but is inaccessible". Somehow I got it to compile by making D1 a struct. I'm using Visual C++.

    Code:
    #include "../std_lib_facilities.h"
    
    class B1
    {
    public:
    	virtual void vf() {cout << "B1::vf\n";}
    	void f() {cout << "B1::f\n";}
    };
    
    class D1 : B1
    {
    public:
    	void vf() {cout << "D1::vf\n";}
    	void f() {cout << "D1::f\n";}
    };
    
    int main()
    {
    	B1 b1;
    	D1 d1;
    	b1.vf();
    	b1.f();
    	d1.vf();
    	d1.f();
    	B1& r = d1;
    
    	keep_window_open();
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And if you do
    Code:
    class D1 : public B1
    instead?

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    Code:
    class D1 : B1
    Unfortunately, this does not do what you think it does. This uses private inheritance, which would make all members in Base inaccessible because they're all inherited as private members.
    The compiler is trying to call an operator in Base, but fails due to that it's private in the derived class. The following demonstrates that it's private inheritance:

    Code:
    class CBase
    {
    public:
    	CBase() { }
    	void myfunc() { }
    };
    
    class CDerived: CBase
    {
    public:
    	CDerived() { }
    };
    
    int main()
    {
    	CDerived d;
    	d.myfunc();
    	return 0;
    }
    This gives the compile error:
    Error 1 error C2247: 'CBase::myfunc' not accessible because 'CDerived' uses 'private' to inherit from 'CBase' g:\w00t\visual studio 2008\projects\test\test.cpp 17

    Now, if you change it to
    Code:
    class CDerived: public CBase
    It will compile fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need a good example of virtual functions
    By Anddos in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2006, 10:48 AM
  2. Virtual Functions
    By guda in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2004, 03:13 PM
  3. Thread creation on virtual functions
    By gustavosserra in forum C++ Programming
    Replies: 13
    Last Post: 10-14-2004, 08:03 AM
  4. recursive virtual functions
    By ygfperson in forum C++ Programming
    Replies: 0
    Last Post: 05-25-2003, 08:00 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

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