Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3669500
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:15:04+00:00 2026-05-19T02:15:04+00:00

I have the following example code: #include <iostream> #include <string> using namespace std; class

  • 0

I have the following example code:

#include <iostream>
#include <string>

using namespace std;

class Event
{
public:
    string type;
    string source;
};

class KeyEvent : public Event
{
public:
    string key;
    string modifier;
};

class MouseEvent : public Event
{
public:
    string button;
    int x;
    int y;
};

void handleEvent(KeyEvent e)
{
    if(e.key == "ENTER")
        cout << "Hello world! The Enter key was pressed ;)" << endl;
}

Event generateEvent()
{
    KeyEvent e;
    e.type = "KEYBOARD_EVENT";
    e.source = "Keyboard0";
    e.key = "SPACEBAR";
    e.modifier = "none";

    return e;
}

int main()
{
    KeyEvent e = generateEvent();

    return 0;
}

I can’t compile it, G++ throws an error of kind:

main.cpp: In function 'int main()':
main.cpp:47:29: error: conversion from 'Event' to non-scalar type 'KeyEvent' requested

I know that the error is obvious for C++ guru’s, but I can’t understand why I can’t do the conversion from base class object to derived one. Can someone suggest me the solution of the problem that I have? Thx in advice

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:15:04+00:00Added an answer on May 19, 2026 at 2:15 am

    Your function generateEvent does the following:

    • creates a KeyEvent
    • converts (upcasts) the object into an Event by copying and slicing
    • returns that Event object

    You then try to take that Event object copy and put it into a KeyEvent again.

    You are trying to use polymorphism but are actually just slicing. Consider (with caution!) dynamic allocation:

    boost::shared_ptr<Event> generateEvent() {
        KeyEvent* e = new KeyEvent;
        e->type = "KEYBOARD_EVENT";
        e->source = "Keyboard0";
        e->key = "SPACEBAR";
        e->modifier = "none";
    
        return boost::shared_ptr<Event>(static_cast<Event*>(e));
    }
    
    int main() {
        boost::shared_ptr<Event> e = generateEvent();
        // you can now use dynamic_cast and/or virtual
        // function calls to treat e as a pointer-to-KeyEvent
    }
    

    Also note that return 0; is implicit in the entrypoint function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have the following situation #include <iostream> class Animal { public: virtual void
I have the following example code: class A(object): def __init__(self, id): self.myid = id
I have the following example class: Test.h: @interface Test : UIButton { NSString *value;
I have tried using the obvious method as outlined in the following example but
Say for example I have the following string: var testString = Hello, world; And
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I currently have the following code: events.detect do |event| #detect does the block until
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
I am a newbie to Python and have come across the following example in
I have following string String str = replace :) :) with some other string;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.