473,388 Members | 993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

exception virtual function through base class

The following code cant call derived::fn() , it is calling base::fn()
which is pure virtual.
(the same problem is ocurring if I do it outside of constructors also)
What am I doing wrong here?

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

class derived
{
public:
void fn(void){};
};

class user
{
public:
user(){
derived d;
p=(base*)new derived();//What is wrong here?
if(p!=0)
p->fn();//exception because p->fn == 0
}
private:
base *p;
};

Jul 6 '06 #1
4 1540
TB
voidtwerp skrev:
The following code cant call derived::fn() , it is calling base::fn()
which is pure virtual.
(the same problem is ocurring if I do it outside of constructors also)
What am I doing wrong here?

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

class derived
class derived : public base
{
public:
void fn(void){};
};

class user
{
public:
user(){
derived d;
p=(base*)new derived();//What is wrong here?
if(p!=0)
p->fn();//exception because p->fn == 0
}
private:
base *p;
};

--
TB @ SWEDEN
Jul 6 '06 #2

voidtwerp wrote:
The following code cant call derived::fn() , it is calling base::fn()
which is pure virtual.
(the same problem is ocurring if I do it outside of constructors also)
What am I doing wrong here?

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

class derived
{
public:
void fn(void){};
};

class user
{
public:
user(){
derived d;
p=(base*)new derived();//What is wrong here?
if(p!=0)
p->fn();//exception because p->fn == 0
}
private:
base *p;
};
you haven't inherited the base class.
p=(base*)new derived();//What is wrong here?
same as above. base and derived are two different classes with no
relation

-- Murali Krishna

Jul 6 '06 #3
Murali Krishna wrote:
>
you haven't inherited the base class.
doh! thanks to TB and Murali for your quick responses - how I managed
to do that in both the original problem and the posted version without
spotting it I dont know.

Jul 6 '06 #4
voidtwerp wrote:
Murali Krishna wrote:
>>you haven't inherited the base class.


doh! thanks to TB and Murali for your quick responses - how I managed
to do that in both the original problem and the posted version without
spotting it I dont know.
The cast was a clue. (I missed it, too, on my first reading)
Jul 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Dario | last post by:
The following simple program behaves differently in Windows and Linux . #include <stdexcept> #include <iostream> #include <string> using namespace std; class LogicError : public logic_error {...
6
by: Páll Ólafsson | last post by:
Hi I have a problem with the Microsoft.ApplicationBlocks.ExceptionManagement? I can't get it to work in RELEASE mode? If I run the project in debug mode the block works fine but when I run the...
11
by: Dave | last post by:
try { ... } catch (exception e) { cout << e.what() << endl; } In the code above, e is caught by value rather than polymorphically (assume
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.