473,322 Members | 1,307 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,322 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 1538
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.