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 9187497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:47:00+00:00 2026-06-17T19:47:00+00:00

I’m fairly new to C++ and this is the problem I have: I have

  • 0

I’m fairly new to C++ and this is the problem I have:
I have two classes, Client and Host. And when everything is loaded you have the option to press two buttons, if you press button 1 Client is loaded and if you press button 2 Host is loaded.

Now both Client and Host are fairly big classes, and I don’t want to put them both into the memory. So my idea was creating a Base class, and then both Client and Host should extend the base class, and then the only thing I had to do was this:

Base connection;

//If button 1 is pressed:
connection = Client();

//If button 2 is pressed:
connection = Host();

Well this sounded almost too good to be true, and when I tried it I got no errors. Now comes the problem, Base has a function called A, and Client has a function called B. So the function B is unique to the class Client.

When I try to call function B I get this error: 'class Base' has no member named 'B'. How can I let C++ know that I am talking to class Client or Host instead of Base? I am also open for a whole new approach to this problem. Maybe it’s just an error in my thinking process.

Thanks in advance!

  • 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-06-17T19:47:01+00:00Added an answer on June 17, 2026 at 7:47 pm

    You ran into a situation which we call object slicing, which is a common problem in languages with value semantics such as C++.

    Object slicing happens when you assign a value of a sub-type to a new location (your variable connection) of a super type. This introduces a new copy of the instance, but of the super type, not the sub-type, so you lose the information about the concrete class you wanted to instantiate.

    To avoid this, you have multiple options.

    The classical approach uses pointers:

    Base * connection;
    connection = new YourConcreteType();
    

    Then, to use this object, you have to derefrerence it using the asterisk operator (*):

    (*connection).function();
    connection->function();    // syntactic sugar, semantically equivalent
    

    Not to forget: You have to delete the object after usage:

    delete connection;
    

    To simplify this, C++ introduces two concepts: references and smart pointers. While the former has a restriction to be only assigned once, it is the syntactically simplest one. The latter is similar to the pointer approach, but you don’t have to care about deletion, so you less likely run into a memory leak situation:

    std::shared_ptr<Base> connection;
    
    connection = make_shared<YourConcreteType>(); // construction via 'make_shared'
    
    // ... use as if it were just a pointer ...
    
    connection->function();
    
    // no delete!
    

    There are also other “smart pointer” types, like unique_ptr, which can be used if you do not intend to pass the pointer around (if it stays in scope).

    Now, you can implement the functions in both classes separately. To make use of polymorphism, this means, during runtime, either the function of the one subclass or of the other subclass is called, depending on which one was constructed, you should declare the functions in the base class as being virtual, otherwise, the function definition in Base will be called, regardless of the concrete type you have constructed.

    In your case, you want to call a function which should do something different, depending on the type. While your approach was to introduce two different functions, namely A and B, you can just declare a single function, let’s call it handleEvent, as a pure virtual (= abstract) function in the base class, which means “this function is to be implemented in sub classes”, and define it in the two subclasses independently:

    Base {
        ....
        virtual void handleEvent(...) = 0; // "= 0" means "pure"
    };
    
    // Don't provide an implementation
    

    Client {
        void handleEvent(...); // override it
    };
    
    // define it for Client:
    void Client::handleEvent(...) {
        ...
    }
    

    Host {
        void handleEvent(...); // override it
    };
    
    // define it for Host:
    void Host::handleEvent(...) {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was
I have been unable to fix a problem with Java Unicode and encoding. The
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.